1. 程式人生 > >自定義view刮刮樂

自定義view刮刮樂

save alias eset validate ack prot 移動 clas tag

package com.example.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.util.Log;
import android.view.MotionEvent;
import android.widget.TextView;

/**
* Created by Administrator on 2017\10\6 0006.
*/

public class ScratchTextView extends TextView {
private static final String TAG = "ScratchTextView";
private Bitmap mbitmap;// 蓋在字上面的圖片
private Canvas mCanvas; //畫線的畫布
private Paint mPaint;//劃線的畫筆
private Path mPath;//線
private float mX, mY;
private float TOUCH_TOLERANCE;
private boolean isInited = false;//用於判斷時候覆蓋了textview的文字

public ScratchTextView(Context context) {
super(context);
}

public ScratchTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ScratchTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

//onDraw初始化的時候調用一次,然invalidate()的時候調用,
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (isInited) {
mCanvas.drawPath(mPath, mPaint);//把線畫到mCanvas上,mCanva會把線畫到mBitmap
canvas.drawBitmap(mbitmap, 0, 0, null);// 把mBitmap畫到textview上 canvas是父textvie傳過來的。
}
}

/**
* 初始化刮刮卡
*
* @param bgColor 刮刮卡背景色,用於蓋住下面的字
* @param paintStrokeWidth 擦除線寬
* @param touchTolerance 畫線容差
*/
public void initScratchCard(final int bgColor, final int paintStrokeWidth, float touchTolerance) {
TOUCH_TOLERANCE = touchTolerance;
mPaint = new Paint();//創建畫筆
mPaint.setAlpha(240);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));//
mPaint.setAntiAlias(true);// 抗鋸齒 mPaint.setDither(true);// 防抖動 mPaint.setStyle(Paint.Style.STROKE);// 畫筆類型: STROKE空心 FILL實心 FILL_AND_STROKE用契形填充 mPaint.setStrokeJoin(Paint.Join.ROUND);// 畫筆接洽點類型 mPaint.setStrokeCap(Paint.Cap.ROUND);// 畫筆筆刷類型 mPaint.setStrokeWidth(paintStrokeWidth);// 畫筆筆刷寬度 mPath = new Path(); //建立一個空的Bit mbitmap = Bitmap.createBitmap(100, 90, Bitmap.Config.ARGB_8888);// mCanvas = new Canvas(mbitmap);//通過bitmap生成一個畫布 Paint paint = new Paint();//用於繪制生成的背景圖片的字體 paint.setTextSize(50);///字體大小 paint.setColor(Color.parseColor("#ff0717")); mCanvas.drawColor(bgColor);//背景色 //.從資源文件中獲取圖片 Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.sss); mCanvas.drawBitmap(photo, 0 , 1 , paint);//設置遮蓋層的圖片。比如說,衣服 //設置外層的文字。 mCanvas.drawText("不要嘛!!", getLayoutParams().width / 4, getLayoutParams().height / 2 + 15, paint); isInited = true; } //該觸膜事件可以定義在調用的activity中實現 @Override public boolean onTouchEvent(MotionEvent event) { if (!isInited) { return true; } switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPath.reset(); // 重置繪制路線,即隱藏之前繪制的軌跡 mPath.moveTo(event.getX(), event.getY()); // mPath繪制的繪制起點 mX = event.getX(); mY = event.getY(); invalidate();//更新界面 Log.d(TAG, mX + "|" + mY); break; case MotionEvent.ACTION_MOVE: //x和y移動的距離 float dx = Math.abs(event.getX() - mX); float dy = Math.abs(event.getY() - mY); //x,y移動的距離大於畫線容差 if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { // 二次貝塞爾,實現平滑曲線;mX, mY為操作點,(x + mX) / 2, (y + mY) / 2為終點 mPath.quadTo(mX, mY, (event.getX() + mX) / 2, (event.getY() + mY) / 2); // 第二次執行時,第一次結束調用的坐標值將作為第二次調用的初始坐標值 mX = event.getX(); mY = event.getY(); Log.d(TAG, mX + "|" + mY); invalidate(); break; } } return true; }}
ScratchTextView scratchTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scratchTv= (ScratchTextView) findViewById(R.id.scratchTv);
String[] str_reward = {"討厭!", "舒服1000!", "真爽10000!","真爽10000!"};
scratchTv.setText(str_reward[getRandom()]);
scratchTv.initScratchCard(0xFFFFFFFF, 20, 1f);
}
private int getRandom() {
Random random = new Random();
int number = random.nextInt(5);
return number;
}
<com.example.guaguale.ScratchTextView
android:id="@+id/scratchTv"
android:layout_width="match_parent"
android:layout_height="match_parent" />

自定義view刮刮樂