首页 > 代码库 > 自定义ImageView->StatusImageView
自定义ImageView->StatusImageView
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。