• PorterDuffXfermodeMode.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.PorterDuff;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.view.View;
    import android.view.animation.LinearInterpolator;
    
    import com.loaderman.customviewdemo.R;
    
    
    public class IrregularWaveView extends View {
    
        private Paint mPaint;
        private int mItemWaveLength = 0;
        private int dx=0;
    
        private Bitmap BmpSRC,BmpDST;
    
        public IrregularWaveView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mPaint = new Paint();
    
            BmpDST = BitmapFactory.decodeResource(getResources(), R.drawable.wave_bg, null);
            BmpSRC = BitmapFactory.decodeResource(getResources(),R.drawable.circle_shape,null);
            mItemWaveLength = BmpDST.getWidth();
    
            startAnim();
        }
        @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        canvas.drawColor(Color.WHITE);
    
        //先画上圆形
        canvas.drawBitmap(BmpSRC,0,0,mPaint);
        //再画上结果
        int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(BmpDST,new Rect(dx,0,dx+BmpSRC.getWidth(),BmpSRC.getHeight()),new Rect(0,0,BmpSRC.getWidth(),BmpSRC.getHeight()),mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(BmpSRC,0,0,mPaint);
        mPaint.setXfermode(null);
        canvas.restoreToCount(layerId);
    }
    
    
    public void startAnim(){
        ValueAnimator animator = ValueAnimator.ofInt(0,mItemWaveLength);
        animator.setDuration(4000);
        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.IrregularWaveView
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

    效果:

  • 相关阅读:
    org.dom4j.DocumentException: Error on line 1 of document: 前言中不允许有内容
    学习过程中的随手笔记
    IT技术团队行而有效的管理之道
    九宫格抽奖HTML+JS版
    Nginx负载均衡深入浅出
    PHP 二维数组根据某个字段排序
    MYSQL INSERT INTO SELECT 不插入重复数据
    小米2成功使用google组件的办法(为了coc游戏能登录google账户)
    PHP 数组排序方法总结
    普通标和转让标的回款和还款日期的算法。
  • 原文地址:https://www.cnblogs.com/loaderman/p/10215191.html
Copyright © 2020-2023  润新知