一、效果圖
先來看看效果圖吧~~
二、實作原理方案
1、自定義ViewGroup-XCDanmuView,繼承RelativeLayout來實作,當然也可以繼承其他三大布局類哈
2、初始化若幹個TextView(彈幕的item View,這裡以TextView為例,當然也可以其他了~),然後通過addView添加到自定義View中
3、通過addView添加到XCDanmuView中,位置在坐标,為了實作 從螢幕外移動進來的效果
我們還需要修改添加進來TextView的位置,以從右向左移動方向來說,addView後必須将該TextView的位置設定到右邊的螢幕外
這樣我們采用的方法,是在onLayout()方法中對childView進行layout重新布局設定位置
4、随機沖左側或右側出來彈幕itemView,移動采用屬性動畫來實作平移,從螢幕的一端移動到另一端,當動畫結束後,就将
該child從XCDanmuView中remove掉。并重新new一個彈幕itemView,并addView到XCDanmuView中,并開始動畫移動
5、本自定義彈幕View支援從左到右和從右到左兩個方向,支援自定義設定螢幕彈幕最多顯示個數。
三、自定義彈幕效果XCDanmuView的具體實作
1、初始化需要用到的資料變量
private int mWidth;
private int mScreenWidth;
private List mChildList;
private boolean mIsWorking = false;
private Context mContext;
private int mMaxShowNum = 15;
private int mRowNum = 4;
private int[] mSpeeds = {
3000,4000,5000,6000
};
private int mDelayDuration = 500;
private int[] mBgResIds = {
R.drawable.bg_danmu0,R.drawable.bg_danmu1,R.drawable.bg_danmu2,R.drawable.bg_danmu3
};
private int[] mRowPos = {
150,140,160,150
};
private Random mRandom;
private String[] mStrContents;
public static enum XCDirection{
FROM_RIGHT_TO_LEFT,FORM_LEFT_TO_RIGHT
}
public enum XCAction{
SHOW,HIDE
}
private XCDirection mDirection = XCDirection.FROM_RIGHT_TO_LEFT;
private void init() {
mScreenWidth = getScreenWidth();
mChildList = new ArrayList<>();
mRandom = new Random();
}
2、初始化若幹個彈幕item view
public void initDanmuItemViews(String[] strContents){
mStrContents = strContents;
for(int i = 0; i < mMaxShowNum; i ++){
int index = mRandom.nextInt(100) % strContents.length;
createDanmuView(i,strContents[index],false);
}
}
3、建立彈幕item view 并addView到XCDanmuView中
public void createDanmuView(int index,String content,boolean reset){
final TextView textView = new TextView(mContext);
textView.setTextColor(Color.WHITE);
int r = mRandom.nextInt(100) % mRowNum;
textView.setBackgroundResource(mBgResIds[r]);
textView.setText(content +"_"+ (index+1));
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
int row = mRandom.nextInt(100) % mRowNum;
while(row == lastRow){
row = mRandom.nextInt(100)% mRowNum;
}
int pos = mRandom.nextInt(100)% mRowNum;
lp.topMargin = row * mRowPos[pos];
lastRow = row;
textView.setLayoutParams(lp);
textView.setPadding(40,2,40,2);
this.addView(textView);
if(reset){
mChildList.set(index,textView);
}else{
mChildList.add(index,textView);
}
textView.setClickable(true);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast toast = Toast.makeText(mContext,textView.getText(),Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,50);
toast.show();
}
});
}
4、重新設定childView的初始位置到螢幕之外
@Override
protected void onLayout(boolean changed,int l,int t,int r,int b) {
super.onLayout(changed,l,t,r,b);
int childCount = this.getChildCount();
for(int i=0;i
View view = getChildAt(i);
RelativeLayout.LayoutParams lp = (LayoutParams) view.getLayoutParams();
if(lp.leftMargin <= 0){
if(mDirection == XCDirection.FORM_LEFT_TO_RIGHT){
view.layout(-view.getMeasuredWidth(),lp.topMargin,lp.topMargin + view.getMeasuredHeight());
}else{
view.layout(mScreenWidth,mScreenWidth+view.getMeasuredWidth(),lp.topMargin+view.getMeasuredHeight());
}
}else{
continue;
}
}
}
5、彈幕item view的移動效果
private Handler mHandler = new Handler() {
@Override
public void handleMessage(final Message msg) {
super.handleMessage(msg);
final int pos = msg.what;
ViewPropertyAnimator animator;
if(mDirection == XCDirection.FROM_RIGHT_TO_LEFT){
animator = mChildList.get(msg.what).animate()
.translationXBy(-(mScreenWidth + mChildList.get(msg.what).getWidth()));
}else{
animator = mChildList.get(msg.what).animate()
.translationXBy(mScreenWidth + mChildList.get(msg.what).getWidth());
}
Random random = new Random(System.currentTimeMillis());
int index = random.nextInt(100) % mSpeeds.length;
animator.setDuration(mSpeeds[index]);
animator.setInterpolator(new LinearInterpolator());
animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
XCDanmuView.this.removeView(mChildList.get(pos));
int index = mRandom.nextInt(100) % mStrContents.length;
createDanmuView(pos,mStrContents[index],true);
mHandler.sendEmptyMessageDelayed(pos,mDelayDuration);
Log.v("czm","size=" + mChildList.size());
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animator.start();
}
};
6、開啟彈幕效果和關閉彈幕效果以及對于的動畫效果
boolean isFirst = true;
public void start(){
switchAnimation(XCAction.SHOW);
if(isFirst){
for(int i =0;i< mChildList.size();i++){
mHandler.sendEmptyMessageDelayed(i,i * mDelayDuration);
}
isFirst = false;
}
mIsWorking = true;
}
public void hide(){
switchAnimation(XCAction.HIDE);
mIsWorking =false;
}
public void stop(){
this.setVisibility(View.GONE);
for(int i =0;i< mChildList.size();i++){
mChildList.get(i).clearAnimation();
mHandler.removeMessages(i);
}
mIsWorking =false;
}
private void switchAnimation(final XCAction action){
AlphaAnimation animation;
if(action == XCAction.HIDE){
animation = new AlphaAnimation(1.0f,0.0f);
animation.setDuration(400);
}else{
animation = new AlphaAnimation(0.0f,1.0f);
animation.setDuration(1000);
}
XCDanmuView.this.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
if(action == XCAction.HIDE){
XCDanmuView.this.setVisibility(View.GONE);
}else{
XCDanmuView.this.setVisibility(View.VISIBLE);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
四、如何使用該自定義側滑View控件
使用該自定義View非常簡單,控件預設效果從右向左,如果需要修改方向為從左到右,隻需設定下方向即可
public class MainActivity extends Activity {
private XCDanmuView mDanmuView;
private List mViewList;
private String[] mStrItems = {
"搜狗","百度","騰訊","360","阿裡巴巴","搜狐","網易","新浪","搜狗-上網從搜狗開始","百度一下,你就知道","必應搜尋-有求必應","好搜-用好搜,特順手","Android-谷歌","IOS-蘋果","Windows-微軟","Linux"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDanmuView();
initListener();
}
private void initListener() {
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mDanmuView.isWorking()) {
mDanmuView.hide();
((Button) view).setText("開啟彈幕");
} else {
mDanmuView.start();
((Button) view).setText("關閉彈幕");
}
}
});
}
private void initDanmuView() {
mDanmuView = (XCDanmuView)findViewById(R.id.danmu);
mDanmuView.initDanmuItemViews(mStrItems);
}
}
五、總結
以上就是在Android中實作自定義彈幕效果的全部内容個,希望本文的内容對大家開發Android的時候能有所幫助。如果有疑問可以留言交流。
總結
如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。
本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
小編個人微信号 jb51ccc
喜歡與人分享程式設計技術與工作經驗,歡迎加入程式設計之家官方交流群!