Android改变图片颜色的自定义控件(十)

Stella981
• 阅读 799

效果如下:

Android改变图片颜色的自定义控件(十)

理解:Xfermode的16总模式如图

Android改变图片颜色的自定义控件(十)

第一步:

package com.rong.activity;

import com.rong.test.R;

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.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
 * 改变图片颜色的自定义View
 * @author 徐荣
 *
 */
public class ChangeImageColorView extends View {
    Paint mPaint;
    PorterDuffXfermode xfermode;
    Bitmap bitmap;
    int mBitWidth;
    int mBitHeight;
    int width;
    int height;
    Rect srcRect;
    Rect dstRect;
    RectF mRect;
    int i=0;

    public ChangeImageColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        // 初始化画笔
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLUE);
        // 设置Xfermode
        xfermode = new PorterDuffXfermode(Mode.SRC_IN);
        // 获取图片源
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.robot);
        mBitWidth = bitmap.getWidth();
        mBitHeight = bitmap.getHeight();
        
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //获取控件的宽高
        width = w;
        height = h;
        srcRect = new Rect(0, 0, width, height);
        dstRect = new Rect(0, 0, width, height);
        mRect = new RectF(0, 0, width, height);
        i=width;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //相当于PS的新建图层
        int num = canvas.saveLayer(0, 0, width, height, mPaint, Canvas.ALL_SAVE_FLAG);
        //绘制图片
        canvas.drawBitmap(bitmap, srcRect, dstRect, mPaint);
        //设置图片混合模式
        mPaint.setXfermode(xfermode);
        //绘制动态区域
        canvas.drawRect(mRect, mPaint);
        //恢复混合模式
        mPaint.setXfermode(null);
        //合并图层
        canvas.restoreToCount(num);
        //更改区域的位置
        i = i - 1;
        mRect.top = i;
        if (i == 0) {
            i = width;
        }
        //刷新UI,他会调用onDraw方法
        invalidate();
    }

}

第二步

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_touchlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <com.rong.activity.ChangeImageColorView
        android:id="@+id/main_percentview"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerInParent="true" />

</RelativeLayout>

Run

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
H5接入app准入报告
功能标准!(http://static.oschina.net/uploads/space/2016/0725/160658_qiJY_1032182.png)!(http://static.oschina.net/uploads/space/2016/0725/160850_gQgJ_1032182.png)iPhone性能标准!
Wesley13 Wesley13
2年前
(适用于)ios 搜索标签列表 商城规格标签列表
效果图: 按钮黑色背景为不可交互 黄色为点击时候的效果 白色为默认颜色!(http://static.oschina.net/uploads/space/2016/0324/231202_f4IS_1186234.gif)h文件////  HomeTagView.h//  YangLand
可莉 可莉
2年前
15道非常经典的Java面试题
试题如下:!(http://static.oschina.net/uploads/space/2016/0729/022902_IBMr_121944.jpg)!(http://static.oschina.net/uploads/space/2016/0729/022923_Exeo_121944.jpg)!(http://static.o
Stella981 Stella981
2年前
AngularJS 学习笔记 (六) 基本概念和用法 之 Service与Provider
1、使用$http服务!(http://static.oschina.net/uploads/space/2016/0623/092916_7mPM_199513.gif)HTML代码:!(http://static.oschina.net/uploads/space/2016/0623/093044_Y
Stella981 Stella981
2年前
Num71 资金债权匹配(撮合)
待匹配资金查询分析:!(http://static.oschina.net/uploads/space/2016/0712/224621_yxKh_2356966.png)待匹配资金查询表结构分析:!(http://static.oschina.net/uploads/space/20
Wesley13 Wesley13
2年前
POI
!(http://static.oschina.net/uploads/space/2016/0120/131416_Dswb_1444646.png)!(http://static.oschina.net/uploads/space/2016/0120/131416_cIpL_1444646.jpg)ApachePOI是Apache(
Stella981 Stella981
2年前
Ngrok搭建环境映射外网访问本地
!(http://static.oschina.net/uploads/space/2016/0105/151215_OnNg_1444646.png)!(http://static.oschina.net/uploads/space/2016/0112/144706_tH0X_1444646.jpg)在开发微信公众号、企业号等需要外
Stella981 Stella981
2年前
Python入门教程之安装MyEclipse插件和安装Python环境
!(http://static.oschina.net/uploads/space/2015/0303/121617_wJt9_1444646.jpg)!(http://static.oschina.net/uploads/space/2016/0120/130908_6m1a_1444646.jpg)http://dlwt.c
Stella981 Stella981
2年前
Discuz X3.1 论坛修改的方法
!(http://static.oschina.net/uploads/space/2016/0120/131055_Yzo0_1444646.png)!(http://static.oschina.net/uploads/space/2016/0120/131056_khNy_1444646.jpg)本人在实际操作中遇到的一些问题。自己
Stella981 Stella981
2年前
Linux环境下软件安装
下载——解压缩:!(http://static.oschina.net/uploads/space/2016/0319/203947_DDF5_2312175.png)把得到的目录放到一个不碍事的目录,我们可以设置隐藏目录:!(http://static.oschina.net/uploads/space/2016/0319/204016