效果優(yōu)化
第一步優(yōu)化:添加水印
很多刮獎(jiǎng)的效果都會(huì)有在刮層上添加自家 logo 做水印效果(這里不知道稱為水印合適嗎?反正就是大概那個(gè)意思)。如下面的支付寶一樣
我們在基礎(chǔ)實(shí)現(xiàn)的第一步中的創(chuàng)建刮層函數(shù)里面添加實(shí)現(xiàn)代碼,同時(shí)也添加一個(gè)自定義屬性和 set 方法可供調(diào)用:
/**
* 設(shè)置水印圖標(biāo)
*
* @param resId 圖標(biāo)資源id,-1表示去除水印
*/
public void setWatermark(int resId) {
if (resId == -1) {
mWatermark = null;
} else {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
mWatermark = new BitmapDrawable(bitmap);
mWatermark.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
}
}
/**
* 創(chuàng)建蒙層
*
* @param width
* @param height
*/
private void createMasker(int width, int height) {
。..
if (mWatermark != null) {//
Rect bounds = new Rect(rect);
mWatermark.setBounds(bounds);
mWatermark.draw(mMaskCanvas);
}
}
實(shí)現(xiàn)效果如下:
當(dāng)然,像效果上還有很多可以進(jìn)行添加,例如還可以加上面支付寶的那種邊緣鋸齒效果等等,這里就各位童鞋自行腦洞實(shí)現(xiàn)啦。
第二步優(yōu)化:添加相應(yīng)事件監(jiān)聽器,以及完善一些常用函數(shù)。
說到事件監(jiān)聽,我想這里莫過于刮獎(jiǎng)完成的事件了吧。對于使用這個(gè)控件的開發(fā)者,肯定需要在刮完之后做相應(yīng)的操作,例如,提示用戶中獎(jiǎng)啦,還是繼續(xù)努力之類的。怎么樣判斷刮獎(jiǎng)完成呢?這里的實(shí)現(xiàn)思路是通過異步計(jì)算刮層 mMaskBitmap 中的像素信息值,通過算得透明像素個(gè)數(shù)占總像素個(gè)數(shù)的比例,當(dāng)這個(gè)比例超過一定閾值的時(shí)候,我們認(rèn)為刮獎(jiǎng)完成了。為什么要說超過一定閾值就算完成,這和現(xiàn)實(shí)生活中刮獎(jiǎng)一樣,你不需要把刮層完全刮得干干凈凈才能得到結(jié)果。當(dāng)然這個(gè)比例是多少,我們同樣需要抽離成可動(dòng)態(tài)設(shè)置的。再添加監(jiān)聽器接口和設(shè)置監(jiān)聽器的 API 即可。實(shí)現(xiàn)代碼,大致如下:
private void onErase() {
int width = getWidth();
int height = getHeight();
new AsyncTask《Integer, Integer, Boolean》() {
@Override
protected Boolean doInBackground(Integer.。. params) {
int width = params[0];
int height = params[1];
int pixels[] = new int[width * height];
mMaskBitmap.getPixels(pixels, 0, width, 0, 0, width, height);//獲取覆蓋圖層中所有的像素信息,stride用于表示一行的像素個(gè)數(shù)有多少
float erasePixelCount = 0;//擦除的像素個(gè)數(shù)
float totalPixelCount = width * height;//總像素個(gè)數(shù)
for (int pos = 0; pos 《 totalPixelCount; pos++) {
if (pixels[pos] == 0) {//透明的像素值為0
erasePixelCount++;
}
}
int percent = 0;
if (erasePixelCount 》= 0 && totalPixelCount 》 0) {
percent = Math.round(erasePixelCount * 100 / totalPixelCount);
publishProgress(percent);
}
return percent 》= mMaxPercent;
}
@Override
protected void onProgressUpdate(Integer.。. values) {
super.onProgressUpdate(values);
mPercent = values[0];
onPercentUpdate();
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result && !mIsCompleted) {//標(biāo)記擦除,并完成回調(diào)
mIsCompleted = true;
if (mEraseStatusListener != null) {
mEraseStatusListener.onCompleted(ScratchView.this);
}
}
}
}.execute(width, height);
}
/**
* 設(shè)置擦除監(jiān)聽器
*
* @param listener
*/
public void setEraseStatusListener(EraseStatusListener listener) {
this.mEraseStatusListener = listener;
}
/**
* 擦除狀態(tài)監(jiān)聽器
*/
public static interface EraseStatusListener {
/**
* 擦除進(jìn)度
*
* @param percent 進(jìn)度值,大于0,小于等于100;
*/
public void onProgress(int percent);
/**
* 擦除完成回調(diào)函數(shù)
*
* @param view
*/
public void onCompleted(View view);
}
我們來看看最終效果
到這里,一個(gè)完整的刮獎(jiǎng)效果自定義控件實(shí)現(xiàn)已經(jīng)完成。不過,這里還有一個(gè)問題需要拋給大家共同思考下,就是在判斷刮獎(jiǎng)是否完成的實(shí)現(xiàn)上,我在代碼中的實(shí)現(xiàn)方式會(huì)創(chuàng)建出大量的 int 數(shù)組,這樣造成后果就是會(huì)產(chǎn)生內(nèi)存抖動(dòng)。
評論