国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android新聞廣告條滾動(dòng)效果

2019-12-12 05:31:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

項(xiàng)目中需要用到類似公告欄的控件,能用的基本不支持多行顯示,于是只好自己動(dòng)手,苦于沒(méi)有自定義過(guò)一個(gè)像樣的控件,借鑒Android公告條demo,實(shí)現(xiàn)了多行向上滾動(dòng)的控件。在原控件基礎(chǔ)之上添加如下功能:
 •傳入數(shù)據(jù)分頁(yè)顯示
 •添加Left Drawable
 •手指觸摸事件處理
 •添加3D動(dòng)畫翻滾效果

效果圖

源碼

package com.android.view;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Camera;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.drawable.Drawable;import android.os.Handler;import android.os.Looper;import android.text.TextPaint;import android.text.TextUtils;import android.util.AttributeSet;import android.util.TypedValue;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AccelerateInterpolator;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.Transformation;import android.view.animation.TranslateAnimation;import android.widget.FrameLayout;import android.widget.LinearLayout;import android.widget.TextView;import com.sd2w.market.client.R;import java.util.ArrayList;import java.util.List;import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;/** * 公告滾動(dòng)區(qū) * * @author 祁連山 * @version 1.0 * @date 2016-08-17 */public class RollingView extends FrameLayout implements OnClickListener { // 默認(rèn)動(dòng)畫執(zhí)行時(shí)間 private static final int ANIMATION_DURATION = 1000; // 延遲滾動(dòng)時(shí)間間隔 private long mDuration = 3000; // 字體顏色 private int mTextColor = 0xff000000; // 點(diǎn)擊后字體顏色 private int mClickColor = 0xff0099ff; // 字體大小 private float mTextSize = 14; // 行間距 private int mTextPadding = 10; // 畫筆 private Paint mPaint; // 默認(rèn)每頁(yè)信息數(shù) private int mPageSize = 3; // 最后一頁(yè)余數(shù) private int mUpLimited = mPageSize; // 當(dāng)前顯示頁(yè)碼 private int mCurrentPage = 0; // 總分頁(yè)數(shù) private int mPageCount; // 左圖片 private int mLeftDrawable; // 分頁(yè)數(shù)據(jù)對(duì)象 private List<LinearLayout> mRollingPages; // 默認(rèn)動(dòng)畫 private AnimationSet mEnterAnimSet; private AnimationSet mExitAnimSet; private RollingRunnable mRunnable; private Handler mHandler; private onItemClickListener mClickListener; // 布局參數(shù) private LayoutParams mFrameParams; private LinearLayout.LayoutParams mLinearParams; //mEnterDownAnim,mOutUp分別構(gòu)成向下翻頁(yè)的進(jìn)出動(dòng)畫 private Rotate3dAnimation mEnterDownAnim; private Rotate3dAnimation mExitUpAnim; //mEnterUpAnim,mOutDown分別構(gòu)成向下翻頁(yè)的進(jìn)出動(dòng)畫 private Rotate3dAnimation mEnterUpAnim; private Rotate3dAnimation mExitDownAnim; public RollingView(Context context) {  this(context, null); } public RollingView(Context context, AttributeSet attrs) {  super(context, attrs);  // 從xml中獲取屬性  TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.RollingView);  mTextSize = array.getDimension(R.styleable.RollingView_textSize, mTextSize);  mTextColor = array.getColor(R.styleable.RollingView_textColor, mTextColor);  array.recycle();  // 創(chuàng)建默認(rèn)顯示隱藏動(dòng)畫  createEnterAnimation();  createExitAnimation();  // 初始化畫筆  mPaint = new TextPaint();  // 初始化Handler對(duì)象  mHandler = new Handler(Looper.getMainLooper());  mEnterDownAnim = createAnim(-90, 0, true, true);  mExitUpAnim = createAnim(0, 90, false, true);  mEnterUpAnim = createAnim(90, 0, true, false);  mExitDownAnim = createAnim(0, -90, false, false); } private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, boolean turnUp) {  final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, turnIn, turnUp);  rotation.setDuration(300);  rotation.setFillAfter(false);  rotation.setInterpolator(new AccelerateInterpolator());  return rotation; } /**  * 設(shè)置分頁(yè)大小  *  * @param pageSize  */ public void setPageSize(int pageSize) {  this.mPageSize = this.mUpLimited = pageSize; } /**  * 設(shè)置延遲時(shí)間  *  * @param millionSeconds  */ public void setDelayedDuration(long millionSeconds) {  this.mDuration = millionSeconds; } /**  * 設(shè)置顯示動(dòng)畫  *  * @param animation  */ public void setEnterAnimation(AnimationSet animation) {  mEnterAnimSet = animation; } /**  * 設(shè)置隱藏動(dòng)畫  *  * @param animation  */ public void setExitAnimation(AnimationSet animation) {  mExitAnimSet = animation; } /**  * 設(shè)置行距  *  * @param padding  */ public void setTextPadding(int padding) {  this.mTextPadding = padding; } /**  * 設(shè)置點(diǎn)擊后字體顏色  *  * @param color  */ public void setClickColor(int color) {  this.mClickColor = color; } /**  * 設(shè)置左圖片  *  * @param drawable  */ public void setLeftDrawable(int drawable) {  this.mLeftDrawable = drawable; } /**  * 設(shè)置點(diǎn)擊事件  *  * @param clickListener  */ public void setOnItemClickListener(onItemClickListener clickListener) {  if (null == clickListener) return;  this.mClickListener = clickListener; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  super.onMeasure(widthMeasureSpec, heightMeasureSpec);  // 如果是未指定大小,那么設(shè)置寬為300px  int exceptWidth = 300;  int exceptHeight = 0;  // 計(jì)算高度,如果將高度設(shè)置為textSize會(huì)很丑,因?yàn)槲淖钟心J(rèn)的上下邊距。  if (MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY) {   if (mTextSize > 0) {    mPaint.setTextSize(mTextSize);    Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();    exceptHeight = (int) (fontMetrics.bottom - fontMetrics.top);   }  }  int width = resolveSize(exceptWidth, widthMeasureSpec);  int height = resolveSize(exceptHeight, heightMeasureSpec);  setMeasuredDimension(width, height); } public void setRollingText(List<String> array) {  if (null == array || array.isEmpty()) return;  this.removeAllViews();  if (mRollingPages == null) {   mRollingPages = new ArrayList<>();  }  mRollingPages.clear();  // 計(jì)算商數(shù)  int quotient = array.size() / mPageSize;  // 計(jì)算余數(shù)  int remainder = array.size() % mPageSize;  // 計(jì)算需要?jiǎng)?chuàng)建多少頁(yè)  mPageCount = remainder == 0 ? quotient : quotient + 1;  for (int i = 0; i < mPageCount; i++) {   // 創(chuàng)建一個(gè)布局   LinearLayout container = createContainer();   if (i == mPageCount - 1) {    mUpLimited = remainder == 0 ? mPageSize : remainder;   }   for (int n = 0; n < mUpLimited; n++) {    TextView textView = createTextView(array.get(mPageSize * i + n));    container.addView(textView);   }   // 添加到分頁(yè)中   mRollingPages.add(container);   this.addView(container);  }  // 初始化顯示第一頁(yè)  mCurrentPage = 0;  mRollingPages.get(mCurrentPage).setVisibility(VISIBLE);  this.setVisibility(mRollingPages.get(mCurrentPage)); } /**  * 創(chuàng)建頁(yè)對(duì)象  *  * @return  */ private LinearLayout createContainer() {  if (mFrameParams == null) {   mFrameParams = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);   mFrameParams.gravity = Gravity.CENTER_VERTICAL;  }  LinearLayout container = new LinearLayout(getContext());  container.setLayoutParams(mFrameParams);  container.setOrientation(LinearLayout.VERTICAL);  return container; } private void setVisibility(LinearLayout container) {  int count = container.getChildCount();  for (int i = 0; i < count; i++) {   container.getChildAt(i).setVisibility(VISIBLE);  } } /**  * 創(chuàng)建頁(yè)內(nèi)容對(duì)象  *  * @param text  * @return  */ private TextView createTextView(String text) {  if (mLinearParams == null) {   mLinearParams = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);   mLinearParams.gravity = Gravity.CENTER_VERTICAL;  }  TextView textView = new TextView(getContext());  textView.setLayoutParams(mLinearParams);  textView.setSingleLine();  textView.setPadding(mTextPadding, mTextPadding, mTextPadding, mTextPadding);  textView.setEllipsize(TextUtils.TruncateAt.END);  textView.setTextColor(mTextColor);  textView.setVisibility(INVISIBLE);  textView.setText(text);  if (mLeftDrawable > 0) {   Drawable drawable = getContext().getResources().getDrawable(mLeftDrawable);   // Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), mLeftDrawable);   // Drawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);   drawable.setBounds(0, 0, 10, 10);   textView.setCompoundDrawablePadding(10);   textView.setCompoundDrawables(drawable, null, null, null);  }  textView.setOnClickListener(this);  // 設(shè)置字體大小  if (mTextSize > 0) {   textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);  }  return textView; } private void createEnterAnimation() {  mEnterAnimSet = new AnimationSet(false);  TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 0, TranslateAnimation.RELATIVE_TO_PARENT, 1f, TranslateAnimation.RELATIVE_TO_SELF, 0f);  AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);  mEnterAnimSet.addAnimation(translateAnimation);  mEnterAnimSet.addAnimation(alphaAnimation);  mEnterAnimSet.setDuration(ANIMATION_DURATION); } private void createExitAnimation() {  mExitAnimSet = new AnimationSet(false);  TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 0, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_PARENT, -1f);  AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);  mExitAnimSet.addAnimation(translateAnimation);  mExitAnimSet.addAnimation(alphaAnimation);  mExitAnimSet.setDuration(ANIMATION_DURATION); } @Override public boolean onTouchEvent(MotionEvent event) {  int action = event.getAction();  switch (action) {   case MotionEvent.ACTION_DOWN:    pause();    break;   case MotionEvent.ACTION_MOVE:   case MotionEvent.ACTION_UP:   case MotionEvent.ACTION_CANCEL:    resume();    break;  }  return true; } public void resume() {  // 只有一頁(yè)時(shí)不進(jìn)行切換  if (mPageCount < 1) return;  if (mRunnable == null) {   mRunnable = new RollingRunnable();  } else {   mHandler.removeCallbacks(mRunnable);  }  mHandler.postDelayed(mRunnable, mDuration); } public void pause() {  if (mRunnable != null) {   mHandler.removeCallbacks(mRunnable);  } } @Override public void onClick(View v) {  if (null == mClickListener) return;  TextView textView = (TextView) v;  mClickListener.onItemClick(textView);  textView.setTextColor(mClickColor); } /**  * 隱藏當(dāng)前頁(yè),顯示下一頁(yè)任務(wù)  */ public class RollingRunnable implements Runnable {  @Override  public void run() {   // 隱藏當(dāng)前頁(yè)   LinearLayout currentView = mRollingPages.get(mCurrentPage);   currentView.setVisibility(INVISIBLE);   if (mExitAnimSet != null) {    currentView.startAnimation(mExitAnimSet);// mExitUpAnim);   }   mCurrentPage++;   if (mCurrentPage >= mPageCount) {    mCurrentPage = 0;   }   // 顯示下一頁(yè)   LinearLayout nextView = mRollingPages.get(mCurrentPage);   nextView.setVisibility(VISIBLE);   setVisibility(nextView);   if (mEnterAnimSet != null) {    nextView.startAnimation(mEnterAnimSet);// mEnterDownAnim);   }   mHandler.postDelayed(this, mDuration);  } } public class Rotate3dAnimation extends Animation {  private final float mFromDegrees;  private final float mToDegrees;  private final boolean mTurnIn;  private final boolean mTurnUp;  private float mCenterX;  private float mCenterY;  private Camera mCamera;  public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {   mFromDegrees = fromDegrees;   mToDegrees = toDegrees;   mTurnIn = turnIn;   mTurnUp = turnUp;  }  @Override  public void initialize(int width, int height, int parentWidth, int parentHeight) {   super.initialize(width, height, parentWidth, parentHeight);   mCamera = new Camera();   mCenterY = getHeight() / 2;   mCenterX = getWidth() / 2;  }  @Override  protected void applyTransformation(float interpolatedTime, Transformation t) {   final float fromDegrees = mFromDegrees;   float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);   final float centerX = mCenterX;   final float centerY = mCenterY;   final Camera camera = mCamera;   final int derection = mTurnUp ? 1 : -1;   final Matrix matrix = t.getMatrix();   camera.save();   if (mTurnIn) {    camera.translate(0.0f, derection * mCenterY * (interpolatedTime - 1.0f), 0.0f);   } else {    camera.translate(0.0f, derection * mCenterY * (interpolatedTime), 0.0f);   }   camera.rotateX(degrees);   camera.getMatrix(matrix);   camera.restore();   matrix.preTranslate(-centerX, -centerY);   matrix.postTranslate(centerX, centerY);  } } public interface onItemClickListener {  void onItemClick(TextView v); }}

使用

// 初始化號(hào)外列表List<String> haowaiArray = new ArrayList<>();haowaiArray.add("[母嬰天地] 買尿不濕送嬰兒手口濕巾");haowaiArray.add("[利民商店] 滿100免費(fèi)配送");haowaiArray.add("[果之家] 泰國(guó)金枕榴蓮8元/kg");haowaiArray.add("[戶外運(yùn)動(dòng)] 戶外運(yùn)動(dòng)專業(yè)裝備搜集");haowaiArray.add("[天天特價(jià)] 只要9.9還包郵");haowaiArray.add("[尖端潮品] 折疊電動(dòng)車");haowaiArray.add("[黑科技] 智能VR帶你裝13");haowaiArray.add("[旅行必備] 太陽(yáng)能充電寶-永不斷電");// 綁定數(shù)據(jù)mRollingView.setPageSize(4);mRollingView.setClickColor(0xff888888);mRollingView.setLeftDrawable(R.drawable.drawable_red_dot);mRollingView.setRollingText(haowaiArray);mRollingView.setOnItemClickListener(this);...@Overridepublic void onItemClick(TextView v) { // handle item click event}@Overridepublic void onResume() { super.onResume(); mRollingView.resume();}@Overridepublic void onPause() { super.onPause(); mRollingView.pause();}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南宁市| 延川县| 郴州市| 南漳县| 霍林郭勒市| 宣城市| 多伦县| 英山县| 桂平市| 祁门县| 额敏县| 江油市| 盘山县| 黔西县| 阳高县| 安溪县| 琼海市| 武鸣县| 毕节市| 阳东县| 江油市| 桂林市| 广汉市| 溆浦县| 青川县| 南京市| 城口县| 明溪县| 景泰县| 安岳县| 德化县| 西畴县| 蓬莱市| 宁晋县| 沈丘县| 五家渠市| 子洲县| 化隆| 竹北市| 玉树县| 灵台县|