PorterDuffXfermode之PorterDuff.Mode.DST_IN

Posted loaderman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PorterDuffXfermode之PorterDuff.Mode.DST_IN相关的知识,希望对你有一定的参考价值。

package com.loaderman.customviewdemo.view;

import android.animation.ValueAnimator;
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.view.View;
import android.view.animation.LinearInterpolator;

import com.loaderman.customviewdemo.R;


public class TextWave_DSTIN extends View {
    private Paint mPaint;
    private Path mPath;
    private int mItemWaveLength = 1000;
    private int dx;

    private Bitmap BmpSRC,BmpDST;
    public TextWave_DSTIN(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPath = new Path();
        mPaint = new Paint();
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        BmpSRC = BitmapFactory.decodeResource(getResources(), R.drawable.text_shade, null);
        BmpDST = Bitmap.createBitmap(BmpSRC.getWidth(), BmpSRC.getHeight(), Bitmap.Config.ARGB_8888);

        startAnim();
    }

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    generageWavePath();

    //先清空bitmap上的图像,然后再画上Path
    Canvas c = new Canvas(BmpDST);
    c.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
    c.drawPath(mPath,mPaint);

    canvas.drawBitmap(BmpSRC,0,0,mPaint);
    int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
    canvas.drawBitmap(BmpDST,0,0,mPaint);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(BmpSRC,0,0,mPaint);
    mPaint.setXfermode(null);
    canvas.restoreToCount(layerId);
}

/**
 * 生成此时的Path
 */
private void generageWavePath(){
    mPath.reset();
    int originY = BmpSRC.getHeight()/2;
    int halfWaveLen = mItemWaveLength/2;
    mPath.moveTo(-mItemWaveLength+dx,originY);
    for (int i = -mItemWaveLength;i<=getWidth()+mItemWaveLength;i+=mItemWaveLength){
        mPath.rQuadTo(halfWaveLen/2,-50,halfWaveLen,0);
        mPath.rQuadTo(halfWaveLen/2,50,halfWaveLen,0);
    }
    mPath.lineTo(BmpSRC.getWidth(),BmpSRC.getHeight());
    mPath.lineTo(0,BmpSRC.getHeight());
    mPath.close();
}

public void startAnim(){
    ValueAnimator animator = ValueAnimator.ofInt(0,mItemWaveLength);
    animator.setDuration(2000);
    animator.setRepeatCount(ValueAnimator.INFINITE);
    animator.setInterpolator(new LinearInterpolator());
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            dx = (Integer)animation.getAnimatedValue();
            postInvalidate();
        }
    });
    animator.start();
}
}
  <com.loaderman.customviewdemo.view.TextWave_DSTIN
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

效果:

技术分享图片

以上是关于PorterDuffXfermode之PorterDuff.Mode.DST_IN的主要内容,如果未能解决你的问题,请参考以下文章

PorterDuffXfermode之PorterDuff.Mode.MULTIPLY

PorterDuffXfermode之PorterDuff.Mode.XOR入门学习

PorterDuffXfermode之Mode.SRC_IN

PorterDuffXfermode之PorterDuff.Mode.DST_IN

Android Paint之 setXfermode PorterDuffXfermode 讲解

随行付数据同步中间件「Porter」开源啦