模拟 手机QQ 点击头部图片 使其变成半透明 使用一张图片实现此效果

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyTextView extends TextView {
	private Context context;

	public MyTextView(Context context) {
		super(context);
		this.context = context;
		initMyButton();
	}

	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initMyButton();
	}

	public MyTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initMyButton();
	}

	private void initMyButton() {
		setBackgroundDrawable(newSelector());
	}

	/**
	 * 传入改变亮度前的bitmap,返回改变亮度后的bitmap
	 * 
	 * @param srcBitmap
	 * @return
	 */
	private Drawable changeBrightnessBitmap(Bitmap srcBitmap) {
		Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(),
				srcBitmap.getHeight(), Config.ARGB_8888);
		// int brightness = 60 - 127;
		ColorMatrix cMatrix = new ColorMatrix();
		cMatrix.set(new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0,// 改变亮度
				0, 0, 1, 0, 0, 0, 0, 0, 0.5f, 0 });
		Paint paint = new Paint();
		paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
		Canvas canvas = new Canvas(bmp);
		// 在Canvas上绘制一个Bitmap
		canvas.drawBitmap(srcBitmap, 0, 0, paint);
		return new BitmapDrawable(bmp);
	}

	/** 设置Selector。 */
	private StateListDrawable newSelector() {
		StateListDrawable bg = new StateListDrawable();
		Drawable normal = getBackground();
		Drawable pressed = changeBrightnessBitmap(((BitmapDrawable) getBackground())
				.getBitmap());
		;
		// View.PRESSED_ENABLED_STATE_SET
		bg.addState(new int[] { android.R.attr.state_pressed,
				android.R.attr.state_enabled }, pressed);
		// View.ENABLED_STATE_SET
		bg.addState(new int[] { android.R.attr.state_enabled }, normal);
		// View.EMPTY_STATE_SET
		bg.addState(new int[] {}, normal);
		return bg;
	}





   <LinearLayout
        android:id="@+id/ll_btn_back"
        android:layout_width="70dip"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:clickable="true"
        android:focusable="true"
        android:gravity="center_vertical" >

        <com.jyt.znjf.intelligentteaching.view.MyButtonView
            android:id="@+id/iv_btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dip"
            android:background="@drawable/btn_back"
            android:duplicateParentState="true" />
    </LinearLayout>










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值