只是一个简单的模仿,记一下,万一以后要用.还需要完善
主要控件就是用的这边的
http://blog.csdn.net/lcq5211314123/article/details/48810121
不过,这里边最下面的按钮 DEMO下载
链接的地址是错的,是个下拉关闭的,所以只能复制代码了.
最烦用这个自定义属性了R.styleable在xml里使用很麻烦,所以就再整理一下
顺便把控件改成三行,因为就用这一处.
package view; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; import com.example.dragrelativelayout.R; /** * 仿格瓦拉App背景自动移动View * 原文这边:http://blog.csdn.net/lcq5211314123/article/details/48810121 */ public class GuevaraView extends View { /**遮罩层透明度百分比*/ private float mAlpha = 0.5f; /** 循环移动的速度*/ private float mSpeed = 4; /**循环移动的背景*/ private Bitmap mBackground0, mBackground1 ,mBackground2; private Context mContext; /** 屏幕宽度 */ private int screenWidth; /**第一排两张轮播图的的横坐标*/ private float x00, x01; /**第二排两张轮播图的的横坐标*/ private float x10, x11; /**第三排两张轮播图的的横坐标*/ private float x20, x21; private int mBgWidth0, mBgHeight0; private int mBgWidth1, mBgHeight1; private int mBgWidth2, mBgHeight2; private Handler mHandler = new Handler() { public void handleMessage(Message msg) { x00 -= mSpeed; x01 -= mSpeed; x10 += mSpeed; x11 += mSpeed; x20 -= mSpeed; x21 -= mSpeed; if (x00 <= -mBgWidth0) {// 把第一张移动到第二张后面 x00 = x01 + mBgWidth0; } if (x01 <= -mBgWidth0) {// 把第二张移动到第一张后面 x01 = x00 + mBgWidth0; } if (x10 >= screenWidth) { x10 = x11 - mBgWidth1; } if (x11 >= screenWidth) { x11 = x10 - mBgWidth1; } if (x20 <= -mBgWidth2) {// 把第一张移动到第二张后面 x20 = x21 + mBgWidth2; } if (x21 <= -mBgWidth2) {// 把第二张移动到第一张后面 x21 = x20 + mBgWidth2; } invalidate(); mHandler.sendEmptyMessageDelayed(0, 100); }; }; public GuevaraView(Context context) { super(context); mContext = context; } public GuevaraView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public void create() { if(mBackground0 == null ) mBackground0 = BitmapFactory.decodeResource(getResources(), R.drawable.aa); if(mBackground1 == null) mBackground1 = BitmapFactory.decodeResource(getResources(), R.drawable.bb); if(mBackground2 == null) mBackground2 = BitmapFactory.decodeResource(getResources(), R.drawable.cc); DisplayMetrics dm = new DisplayMetrics(); WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; mBgWidth0 = mBackground0.getWidth(); mBgWidth1 = mBackground1.getWidth(); mBgWidth2 = mBackground2.getWidth(); mBgHeight0 = mBackground0.getHeight(); mBgHeight1 = mBackground1.getHeight(); mBgHeight2 = mBackground2.getHeight(); x00 = 0; x01 = mBgWidth0; x10 = screenWidth - mBgWidth1; x11 = screenWidth - mBgWidth1 - mBgWidth1; x20 = 0; x21 = mBgWidth2; mHandler.sendEmptyMessageDelayed(0, 0); } public void start() { mHandler.sendEmptyMessageDelayed(0, 0); } public void stop(){ mHandler.removeMessages(0); } public boolean isStop(){ return !mHandler.hasMessages(0); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 指定View的宽度为屏幕宽度,高度为背景高度。 */ int viewHeight = mBgHeight0 +mBgHeight1 +mBgHeight2 + getPaddingTop() + getPaddingBottom(); setMeasuredDimension(screenWidth, viewHeight); } @Override protected void onDraw(Canvas canvas) { /** * 绘制两张背景图 */ canvas.drawBitmap(mBackground0, x00, 0, null); canvas.drawBitmap(mBackground0, x01, 0, null); canvas.drawBitmap(mBackground1, x10, mBgHeight0, null); canvas.drawBitmap(mBackground1, x11, mBgHeight0, null); canvas.drawBitmap(mBackground2, x20, mBgHeight0+mBgHeight1, null); canvas.drawBitmap(mBackground2, x21, mBgHeight0+mBgHeight1, null); /** * 绘制遮罩层 */ canvas.drawARGB((int) (mAlpha * 255), 0, 0, 0); } @Override protected void onDetachedFromWindow() {// 退出时销毁 super.onDetachedFromWindow(); mBackground0 = null; mBackground1 = null; mBackground2 = null; } /**遮罩层透明度百分比*/ public void setAlpha(float mAlpha){ this.mAlpha = mAlpha; } /** 循环移动的速度*/ public void setSpeed(float mSpeed){ this.mSpeed = mSpeed; } public void setDrawable(int id0 ,int id1 ,int id2){ mBackground0 = BitmapFactory.decodeResource(getResources(), id0); mBackground1 = BitmapFactory.decodeResource(getResources(), id1); mBackground2 = BitmapFactory.decodeResource(getResources(), id2); } }
试了一下,没问题,能运行了.
直接用别人博客的demo,把这个给加上去了.
可以到这边下载 http://down.51cto.com/data/2219589
时间: 2024-10-12 16:54:55