您好,登錄后才能下訂單哦!
本文實例為大家分享了Android studio實現刮刮樂的具體代碼,供大家參考,具體內容如下
MainActivity
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
第一種方法:
GuaTwo
public class GuaTwo extends View { /*第一種方法*/ private Path mPath;//手刮動的path,過程 private Paint mOutterPaint;//繪制mPath的畫筆 private Canvas mCanvas;//臨時畫布 private Bitmap mBitmap;//臨時圖片 //記錄用戶path每次的開始坐標值 private int mLastX; private int mLastY; private Bitmap mOutterBitmap;//圖片遮罩,就是手刮動,要擦掉的那張圖 public GuaTwo(Context context) { this(context, null); } public GuaTwo(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GuaTwo(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //獲得控件的寬高 int width = getMeasuredWidth(); int height = getMeasuredHeight(); //初始化bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); //設置畫筆屬性 setupOutPaint(); mCanvas.drawColor(Color.parseColor("#c0c0c0")); } @Override protected void onDraw(Canvas canvas) { mOutterPaint.setStyle(Paint.Style.STROKE); mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就類似橡皮檫,這個屬性設置是關鍵 canvas.drawBitmap(mOutterBitmap, 0, 0, null); canvas.drawBitmap(mBitmap, 0, 0, null); mCanvas.drawPath(mPath, mOutterPaint); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int x = (int) event.getX(); int y = (int) event.getY(); switch (action) { case MotionEvent.ACTION_DOWN://按下 //記錄按下的時候的X和Y值,以便于之后移動的時候繪制 mLastX = x; mLastY = y; mPath.moveTo(mLastX, mLastY); break; case MotionEvent.ACTION_MOVE://移動 //拿到用戶移動的X絕對值,Y軸絕對值 int dx = Math.abs(x - mLastX); int dy = Math.abs(y - mLastY); //用戶滑動超過3像素才會改變,這個可以不做,做只是為了避免很頻繁的響應而已。 if (dx > 3 || dy > 3) { mPath.lineTo(x, y); } mLastX = x; mLastY = y; break; } invalidate();//刷新UI return true; } /** * 繪制path(也就是手刮動的path來繪制) 的畫筆屬性 * 類似橡皮擦 */ private void setupOutPaint() { mOutterPaint.setColor(Color.RED); mOutterPaint.setAntiAlias(true); mOutterPaint.setDither(true); mOutterPaint.setStrokeJoin(Paint.Join.ROUND);//設置圓角 mOutterPaint.setStrokeCap(Paint.Cap.ROUND); mOutterPaint.setStyle(Paint.Style.FILL); mOutterPaint.setStrokeWidth(60);//設置畫筆寬度 } /** * 初始化信息 */ private void init() { mOutterPaint = new Paint(); mPath = new Path(); mOutterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mein); }
第二種方法:
GuaTwo
private Path mPath;//手刮動的path,過程 private Paint mOutterPaint;//繪制mPath的畫筆 private Canvas mCanvas; private Bitmap mBitmap; //記錄用戶path每次的開始坐標值 private int mLastX; private int mLastY; private Bitmap mOutterBitmap;//圖片遮罩,就是手刮動,要擦掉的那張圖 private String mText;//刮獎文本信息 private Rect mTextBound; private Paint mBackPaint;//刮獎信息的畫筆 public GuaTwo(Context context) { this(context, null); } public GuaTwo(Context context, AttributeSet attrs) { this(context, attrs, 0); } public GuaTwo(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //獲得控件的寬高 int width = getMeasuredWidth(); int height = getMeasuredHeight(); //初始化bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap);//用指定的位圖構造一個畫布來繪制。 //設置畫筆屬性 setupOutPaint(); setUpBackPaint(); // mCanvas.drawColor(Color.parseColor("#c0c0c0")); mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30, mOutterPaint);//用mOutterPaint畫圓角矩形 mCanvas.drawBitmap(mOutterBitmap, null, new Rect(0, 0, width, height), null);//在剛剛畫的圓角矩形上面再畫一個bitmap圖片,讓圖片大小和圓角矩形大小相關聯 } @Override protected void onDraw(Canvas canvas) { mOutterPaint.setStyle(Paint.Style.STROKE); mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就類似橡皮檫,這個屬性設置是關鍵 canvas.drawText(mText, (getWidth() - mTextBound.width()) / 2, getHeight() / 2 - mTextBound.height() / 2, mBackPaint);//把獲獎信息放在正中間 mCanvas.drawPath(mPath, mOutterPaint); canvas.drawBitmap(mBitmap, 0, 0, null); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); int x = (int) event.getX(); int y = (int) event.getY(); switch (action) { case MotionEvent.ACTION_DOWN://按下 //記錄按下的時候的X和Y值,以便于之后移動的時候繪制 mLastX = x; mLastY = y; mPath.moveTo(mLastX, mLastY); break; case MotionEvent.ACTION_MOVE://移動 //拿到用戶移動的X絕對值,Y軸絕對值 int dx = Math.abs(x - mLastX); int dy = Math.abs(y - mLastY); //用戶滑動超過3像素才會改變,這個可以不做,做只是為了避免很頻繁的相應而已。 if (dx > 3 || dy > 3) { mPath.lineTo(x, y); } mLastX = x; mLastY = y; break; } invalidate();//刷新UI return true; } private void setUpBackPaint() { mBackPaint.setColor(Color.RED); mBackPaint.setStyle(Paint.Style.FILL); mBackPaint.setTextSize(60); //獲得當前畫筆繪制文本的寬和高 mBackPaint.getTextBounds(mText, 0, mText.length(), mTextBound); } *//** * 繪制path(也就是手刮動的path來繪制) 的畫筆屬性 * 類似橡皮擦 *//* private void setupOutPaint() { mOutterPaint.setColor(Color.RED); mOutterPaint.setAntiAlias(true); mOutterPaint.setDither(true); mOutterPaint.setStrokeJoin(Paint.Join.ROUND);//設置圓角 mOutterPaint.setStrokeCap(Paint.Cap.ROUND); mOutterPaint.setStyle(Paint.Style.FILL); mOutterPaint.setStrokeWidth(60);//設置畫筆寬度 } *//** * 初始化信息 *//* private void init() { mOutterPaint = new Paint(); mPath = new Path(); mOutterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.huahua); mText = "您中獎了!"; mTextBound = new Rect(); mBackPaint = new Paint(); }
布局文件
xml文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.bwie.test.guaguale.MainActivity"> <com.bwie.test.guaguale.GuaTwo android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。