編輯:關於Android編程
思想:
將一個View設計成多層,內層(包括中獎信息)和外層(用於刮獎),外層的圖層用Canvas與一個Bitmap關聯,用這個關聯的Bitmap來處理手勢的滑動,類似於刮獎的動作。
使用paint.setXfermode 來進行消除手勢滑動區域
package com.jackie.guaguale; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by Administrator on 2015/7/29. */ public class GuaView extends View { private Path mPath; private Paint mInnerPaint; //內層圖層Paint private Paint mOuterPaint; //外層圖層Paint private Bitmap mGuaBitmap; //用於處理刮獎的Bitmap private Bitmap mOuterBitmap; //外層圖層Bitmap private Canvas mCanvas; private int mWidth, mHeight; private float mLastX; private float mLastY; private String mText; public GuaView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mPath = new Path(); mOuterPaint = new Paint(); mInnerPaint = new Paint(); //創建外層圖層 mOuterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.guaguaka).copy(Bitmap.Config.ARGB_8888, true); mText = ¥500; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = mOuterBitmap.getWidth(); mHeight = mOuterBitmap.getHeight(); //創建內層圖層 mGuaBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mGuaBitmap); mCanvas.drawBitmap(mOuterBitmap, 0, 0, null); //將mOuterBitmap畫到mCanvas上,與mGuaBitmap關聯 setOuterPaint(); setInnerPaint(); } private void setInnerPaint() { mInnerPaint.setColor(Color.RED); mInnerPaint.setStyle(Paint.Style.STROKE); mInnerPaint.setStrokeCap(Paint.Cap.ROUND); mInnerPaint.setStrokeJoin(Paint.Join.ROUND); mInnerPaint.setAntiAlias(true); mInnerPaint.setDither(true); //防抖 mInnerPaint.setStrokeWidth(5); mInnerPaint.setTextSize(100); mInnerPaint.setTextAlign(Paint.Align.CENTER); } private void setOuterPaint() { mOuterPaint.setColor(Color.GREEN); mOuterPaint.setStyle(Paint.Style.STROKE); mOuterPaint.setStrokeCap(Paint.Cap.ROUND); mOuterPaint.setStrokeJoin(Paint.Join.ROUND); mOuterPaint.setAntiAlias(true); mOuterPaint.setDither(true); //防抖 mOuterPaint.setStrokeWidth(20); } @Override //Path public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastX = x; mLastY = y; mPath.moveTo(x, y); break; case MotionEvent.ACTION_MOVE: float deltaX = Math.abs(x - mLastX); float deltaY = Math.abs(y - mLastY); if (deltaX > 5 || deltaY > 5) { mPath.lineTo(x, y); } mLastX = x; mLastY = y; break; case MotionEvent.ACTION_UP: break; } invalidate(); return true; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.parseColor(#bbbbbb)); //背景底色 灰色 canvas.drawText(mText, mWidth / 2, mHeight / 4 * 3, mInnerPaint); //繪制文本 canvas.drawBitmap(mGuaBitmap, 0, 0, null); //繪制外層Bitmap, 將mBitmap顯示在界面上 drawPath(); } private void drawPath() { //使用該mode:dst和src相交後, 只保留dst,且除去相交的部份 mOuterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); mCanvas.drawPath(mPath, mOuterPaint); } }
自Android 4.4起,Android中的WebView開始基於Chromium( 這大概是因為Android部門負責人從Andy Rubin變成了Chrome部門的
Android Overview Screen – 概覽界面原文鏈接:http://developer.android.com/guide/component
關於布局動畫是針對ViewGroup而言的,意指ViewGroup在增加子View或者刪除子View時其子View的過渡動畫,在android官網有這麼一個簡單的例子,其
Android是基於Java的,所以也分主線程,子線程!主線程:實現業務邏輯、UI繪制更新、各子線程串連,類似於將軍;子線程:完成耗時(聯網取數據、SD卡數據加載、後台長