導(dǎo)航
Paint API之—— Xfermode與PorterDuff全面詳解
Paint API之—— Xfermode與PorterDuff詳解(三)動(dòng)畫效果
1.要實(shí)現(xiàn)的效果圖以及實(shí)現(xiàn)流程分析:
要實(shí)現(xiàn)的效果圖:
實(shí)現(xiàn)流程分析:
Step 1.首先齿梁,一個(gè)文字圖片(透明背景)
Step 2.初始化畫筆沫勿,背景圖片(DST),矩形Rect(SRC)
Step 3.先保存圖層,接著先繪制背景圖库倘,設(shè)置混排模式,然后繪制Rect论矾,清除混排模式 接著回復(fù)保存的圖層教翩,最后修改下Rect區(qū)域高度,調(diào)用invalidate()讓View重繪贪壳!
如果流程分析有點(diǎn)不懂饱亿,直接看代碼,超簡(jiǎn)單~
2.代碼實(shí)現(xiàn):
首先是屏幕工具類闰靴,ScreenUtil.java彪笼,這里就不貼了,之前的幾節(jié)中有貼過(guò)蚂且! 然后是我們的自定義View類:LoadTextView.java:
public class LoadTextView extends View {
private PorterDuffXfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
private Bitmap backBitmap;
private Paint mPaint;
private int mBitW, mBitH;
private int mCurW, mCurH, mCurTop;
private Rect mDynamicRect;
public LoadTextView(Context context) {
this(context, null);
}
public LoadTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mCurW = ScreenUtil.getScreenW(context);
mCurH = ScreenUtil.getScreenH(context);
init();
}
public LoadTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
//畫筆初始化:
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setFilterBitmap(true);
mPaint.setDither(true);
mPaint.setColor(Color.RED);
//背部圖片的初始化
backBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.img_string);
mBitH = backBitmap.getHeight();
mBitW = backBitmap.getWidth();
//設(shè)置當(dāng)前的高度
mCurTop = mBitH;
mDynamicRect = new Rect(0, mBitH, mBitW, mBitH); //初始化原圖
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int saveLayerCount = canvas.saveLayer(0, 0, mCurW, mCurH, mPaint, Canvas.ALL_SAVE_FLAG);
canvas.drawBitmap(backBitmap, 0, 0, mPaint);// 繪制目標(biāo)圖
mPaint.setXfermode(mXfermode); //設(shè)置混排模式
canvas.drawRect(mDynamicRect, mPaint); //繪制源圖
mPaint.setXfermode(null); //清除混排模式
canvas.restoreToCount(saveLayerCount); //恢復(fù)保存的圖層
// 改變Rect區(qū)域配猫,假如
mCurTop -= 2;
if (mCurTop <= 0) {
mCurTop = mBitH;
}
mDynamicRect.top = mCurTop;
invalidate(); //重繪
}
}
就是這么簡(jiǎn)單