码迷,mamicode.com
首页 > 其他好文 > 详细

自定义ImageView->StatusImageView

时间:2014-11-20 18:26:22      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   ar   color   使用   sp   

package com.example.customshapedemo;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class StatusImageView extends ImageView {
    Paint paint = new Paint();
    boolean click_status=false;
    int color=Color.BLACK;
    int alpha=100;
    public StatusImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public StatusImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        initParam(context, attrs);
    }

    public StatusImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        initParam(context, attrs);
    }
    //    解析参数
    void initParam(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.StatusImageView);
        color= typedArray.getColor(0, Color.BLACK);
        typedArray.recycle();
    }

    //根据tag来判断绘画点击和未点击的效果
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        if(click_status){
            Bitmap b=((BitmapDrawable)getDrawable()).getBitmap();
            //绘制原图
            canvas.drawBitmap(b, 0, 0, null);
            //去去掉锯齿
            paint.setAntiAlias(true);
            //设置颜色
            paint.setColor(color);
            //设置透明度
            paint.setAlpha(alpha);
            //设置层次的类型
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
            //创建一个同样大小的矩形,然后用画笔遮盖上去
            RectF rect = new RectF(0, 0, b.getWidth(), b.getHeight());
            canvas.drawRect(rect, paint);
        }else{
            super.onDraw(canvas);
        }
    }
    //更具点击事件,修改Tag,并且重绘界面
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        System.out.println(color);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            click_status=true;
            break;
        case MotionEvent.ACTION_UP:
            click_status=false;
            break;
        default:
            break;
        }
        invalidate();
        return true;
    }
    
}

在values/styles.xml中添加自定义属性:

    <declare-styleable name="StatusImageView">
        <attr name="color" format="reference|color" />
    </declare-styleable>

xml文件中定义使用自定义类 引入namespace,以及用报名+类名的形式使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:customshape="http://schemas.android.com/apk/res/com.example.customshapedemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <com.example.customshapedemo.CustomImgeView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="44dp"
        android:layout_toRightOf="@+id/textView1"
        android:src="@drawable/ddw1"
        customshape:radius="15"
        customshape:type="round" >
    </com.example.customshapedemo.CustomImgeView>

    <com.example.customshapedemo.CustomImgeView
        android:id="@+id/customImgeView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="119dp"
        android:src="@drawable/ddw1"
        customshape:type="circle" />

    <com.example.customshapedemo.StatusImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="40dp"
        android:src="@drawable/ddw1"
        customshape:color="#FF6103" />

    <com.example.customshapedemo.StatusImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/customImgeView1"
        android:layout_below="@+id/customImgeView1"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="22dp"
        android:src="@drawable/ddw1"
        customshape:color="#123456" />

</RelativeLayout>

 

自定义ImageView->StatusImageView

标签:android   style   blog   http   io   ar   color   使用   sp   

原文地址:http://www.cnblogs.com/yangqian/p/4111158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!