本文主要是介绍自定义控件(3)---LightingColorFilter光照颜色过滤器--Selector的效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
源码就是博客里的这些文件,没有上传代码,就差一个图片而已,效果类似图片的selector切换
LightingColorFilter顾名思义光照颜色过滤LightingColorFilter (int mul, int add)
mul全称是colorMultiply意为色彩倍增,而add全称是colorAdd意为色彩添加,这两个值都是16进制的色彩值0xAARRGGBB
LightingColorFilter(0xFFFFFFFF, 0x00000000)的时候原图是不会有任何改变的,如果我们想增加红色的值,那么LightingColorFilter(0xFFFFFFFF, 0x00XX0000)就好,其中XX取值为00至FF
0xFFFFFF00 0x表示16进制数,前两个ff表示的是透明度,00-ff,接下来两个ff表示红R,00-ff,后面两个ff表示绿G,00-ff,最后两个ff表示蓝B,00-ff
黑色0x00000000
白色0x00ffffff
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical" ><com.aigestudio.customviewdemo.views.CustomViewandroid:id="@+id/main_cv"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>
MeasureUtil
package com.aigestudio.customviewdemo.views;import android.app.Activity;
import android.util.DisplayMetrics;/*** 测绘工具类*/
public final class MeasureUtil {/*** 获取屏幕尺寸* * @param activity* Activity* @return 屏幕尺寸像素值,下标为0的值为宽,下标为1的值为高*/public static int[] getScreenSize(Activity activity) {DisplayMetrics metrics = new DisplayMetrics();activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);return new int[] { metrics.widthPixels, metrics.heightPixels };}
}
MainActivity
package com.aigestudio.customviewdemo.activities;import android.app.Activity;
import android.os.Bundle;import com.aigestudio.customviewdemo.R;
import com.aigestudio.customviewdemo.views.CustomView;public class MainActivity extends Activity {private CustomView mCustomView;// 我们的自定义View@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取控件mCustomView = (CustomView) findViewById(R.id.main_cv);}
}
CustomView
package com.aigestudio.customviewdemo.views;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;import com.aigestudio.customviewdemo.R;public class CustomView extends View {private Paint mPaint;// 画笔private Context mContext;// 上下文环境引用private Bitmap bitmap;// 位图private int x, y;// 位图绘制时左上角的起点坐标private boolean isClick;// 用来标识控件是否被点击过,默认没有被点击 falsepublic CustomView(Context context) {this(context, null);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;// 初始化画笔initPaint();// 初始化资源initRes(context);this.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {/** 判断控件是否被点击过*/if (isClick) {mPaint.setColorFilter(null);isClick = false;} else {// 点击后变为红色mPaint.setColorFilter(new LightingColorFilter(0xFFFFFFFF,0X00FF0000));isClick = true;}// 记得重绘invalidate();}});}/*** 初始化画笔*/private void initPaint() {// 实例化画笔mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);}/*** 初始化资源*/private void initRes(Context context) {// 获取位图bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.a2);/** 计算位图绘制时左上角的坐标使其位于屏幕中心 屏幕坐标x轴向左偏移位图一半的宽度 屏幕坐标y轴向上偏移位图一半的高度*/x = MeasureUtil.getScreenSize((Activity) mContext)[0] / 2- bitmap.getWidth() / 2;y = MeasureUtil.getScreenSize((Activity) mContext)[1] / 2- bitmap.getHeight() / 2;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 绘制位图canvas.drawBitmap(bitmap, x, y, mPaint);}
}
这篇关于自定义控件(3)---LightingColorFilter光照颜色过滤器--Selector的效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!