編輯:關於Android編程
今天整理之前的代碼,忽然看到之前自己寫的一個刮刮卡,整理下以便以後使用,同時分享給需要的朋友,如有錯誤,還請多多指正。


實現的步驟,其實就是徒手畫三個圖層疊加在一起,最上層是繪制需要的問題,就是以上所述的“騷年,刮我吧”,第二層就是覆蓋寬高的灰層,第三層是結果層,多的不啰嗦了,具體實現如下,附上詳細注釋。
/**
*
* created by zero on 2016-9-9
*
* 刮刮卡
*
*/
public class ScratchView extends View
{
public ScratchView(Context context)
{
super(context);
init();
}
private Canvas mCanvas = null;
private Path mPath = null;
private Paint mPaint = null;
// 定義畫布的寬和高
private int screenWidth = 720;
private int screenHeight = 360;
private Bitmap bitmap = null;
private void init() {
// TODO Auto-generated method stub
mPath = new Path();
bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Config.ARGB_8888);
// 對mPaint的設置
mPaint = new Paint();
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mCanvas = new Canvas();
mPaint.setDither(true);
// 設置畫筆為空心
mPaint.setStyle(Style.STROKE);
// 設置線寬,即每次擦除的寬度
mPaint.setStrokeWidth(10);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setStrokeJoin(Join.ROUND);
// 設置圖形重疊時的處理方式,一共有16種方式,有興趣可自己查閱
mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
mPaint.setAlpha(0);
mCanvas = new Canvas(bitmap);
mCanvas.drawColor(Color.parseColor("#c0c0c0"));
setBitmapText();
}
private void setBitmapText() {
Paint paint = new Paint();
paint.setTextSize(40);
paint.setColor(Color.parseColor("#9f9fa0"));
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
paint.setFakeBoldText(true);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.alpha(0));
canvas.rotate(-20);
// 遍歷繪制文字
for (int i = 0; i < screenWidth + 200; i += 300)
{
for (int j = 0; j < screenHeight + 200; j += 60)
{
canvas.drawText("刮我吧,騷年!", i, j, paint);
}
}
setScratchBackground("一等獎");
}
// 接收後台傳來的文字,即中獎或者未中獎的文字
public void setScratchBackground(String txt_win) {
// TODO Auto-generated method stub
Paint paint = new Paint();
Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Config.ARGB_8888);
paint.setTextSize(40);
paint.setColor(Color.BLACK);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.alpha(0));
canvas.drawText(txt_win, screenWidth / 2, 60, paint);
setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mCanvas.drawPath(mPath, mPaint);
canvas.drawBitmap(bitmap, 0, 0, null);
}
int x = 0;
int y = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
int currX = (int) event.getX();
int currY = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
mPath.reset();
x = currX;
y = currY;
mPath.moveTo(x, y);
}
break;
case MotionEvent.ACTION_MOVE:
{
mPath.quadTo(x, y, currX, currY);
x = currX;
y = currY;
postInvalidate();
}
break;
case MotionEvent.ACTION_UP:
{
new Thread(mRunnable).start();
}
case MotionEvent.ACTION_CANCEL:
{
mPath.reset();
}
break;
}
return true;
}
private Runnable mRunnable = new Runnable()
{
private int[] mPixels;
@Override
public void run() {
float wipeArea = 0;
float totalArea = screenWidth * screenHeight;
Bitmap mBitmap = bitmap;
mPixels = new int[screenWidth * screenHeight];
/**
* 拿到所有的像素信息
*/
mBitmap.getPixels(mPixels, 0, screenWidth, 0, 0, screenWidth,
screenHeight);
/**
* 遍歷統計擦除的區域
*/
for (int i = 0; i < screenWidth; i++)
{
for (int j = 0; j < screenHeight; j++)
{
int index = i + j * screenWidth;
if (mPixels[index] == 0)
{
wipeArea++;
}
}
}
/**
* 根據所占百分比,進行一些操作
*/
if (wipeArea > 0 && totalArea > 0)
{
int percent = (int) (wipeArea * 100 / totalArea);
/**
* 設置達到多少百分比的時候,彈窗提醒是否中獎此處設置為20
*/
if (percent > 20)
{
/**
* 刮開獎以後的操作,此處在子線程toast,可能會發生線程阻塞,只為測試使用
*/
Looper.prepare();
Toast.makeText(getContext(), "已刮開" + percent + "%",
Toast.LENGTH_LONG).show();
Looper.loop();
}
}
}
};
}
發的是公司需要的效果,以上代碼只是一個實現,各種樣式還需要自己去實現。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android中Xposed框架篇---修改系統位置信息實現自身隱藏功能實例
一、前言本文主要來介紹一個實際案例就是如何通過這個框架來修改系統的地理位置信息來實現隱藏功能,在如今社交工具的發展特別是微信,他有一個實時位置共享功能,那麼對於那些不是單
Android 全仿To圈兒錄音界面實現
我們先來看看To圈(QQ,微信等其他大部分軟件也是大同小異)的注冊錄音界面運行截圖:(⊙o⊙)…為了實現這個效果還是花了一番功夫的,主要難點有以下方面:1、
android學習八(ListView的高級使用)
ListView在android開放中用的比較多,所以接下來就進行ListView的使用的講解。 首先創建一個android項目,項目名為ListViewTest. Li
Qt qml中listview 列表視圖控件(下拉刷新、上拉分頁、滾動軸)
Qt qml listview下拉刷新和上拉分頁主要根據contentY來判斷。但要加上頂部下拉指示器、滾動條,並封裝成可簡單調用的組件,著實花了我不少精力:)先給大家展