Android之自定义点击效果 .

来源:互联网 发布:windows samba服务器 编辑:程序博客网 时间:2024/05/16 08:33

         最近遇到需要点击图片按钮看到点击效果的情节,有效的避免重复点击。

自定义点击效果主要有三种方式:

        1、由图片替换显示效果

         首先准备两张图片,分别是按下和离开时的图片,并且在drawable下新建selector.xml文件,如图:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/dashiji_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/dashiji" android:state_focused="false" android:state_pressed="false"/>
    <item android:drawable="@drawable/dashiji_press"  android:state_focused="true"/>
    <item android:drawable="@drawable/dashiji" android:state_focused="false"/>
</selector>

在布局文件中对图片按钮与以上文件进行绑定:

            <ImageView
                android:layout_width="@dimen/navigate_image_height_small"
                android:layout_height="@dimen/navigate_image_height_small"
                android:id="@+id/dashiji"
                android:gravity="center_horizontal"
                android:background="@drawable/dashiji_selectable"
                android:adjustViewBounds="true"
                android:layout_alignParentBottom="true" />

在activity中调用点击事件:

    @ViewById(R.id.dashiji)
    ImageView mDashijiView;

mDashijiView.setOnClickListener(this);

 @Override
    public void onClick(View v) {
       if(v==mDashijiView){

           

        }

}

以上方法经使用真实有效。

下面还有两种方法供参考,鄙人使用时遇到些问题,也许是鄙人使用方法有误,也许对您有帮助:

与以上方法类似,省去selector.xml文件,将实现方法移到Activity中:

 Button button2=(Button) this.findViewById(R.id.button2);        button2.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(event.getAction()==MotionEvent.ACTION_DOWN){v.setBackgroundResource(R.drawable.button_press);}else if(event.getAction()==MotionEvent.ACTION_UP){v.setBackgroundResource(R.drawable.button_nomal);}return false;}});

 

2、

android view增加点击效果,如点击时变暗。

使ImageButton有按下的特效,只需要准备一张普通的图片,不需要按下效果的图片

package org.animation;

import android.graphics.ColorMatrixColorFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class AnimationUtils {
 
 /**
  * 给试图添加点击效果,让背景变深
  * */
 public static void addTouchDrak(View view , boolean isClick){
  view.setOnTouchListener( VIEW_TOUCH_DARK ) ;
  
  if(!isClick){
   view.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
    }
   });
  }
 }
 
 /**
  * 给试图添加点击效果,让背景变暗
  * */
 public static void addTouchLight(View view , boolean isClick){
  view.setOnTouchListener( VIEW_TOUCH_LIGHT ) ;
  
  if(!isClick){
   view.setOnClickListener(new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
    }
   });
  }
 }
 
 
 /**
  * 让控件点击时,颜色变深
  * */
 public static final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {

  public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, -50, 0, 1,
    0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };
  public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };

  @Override
  public boolean onTouch(View v, MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
    if(v instanceof ImageView){
     ImageView iv = (ImageView) v;
     iv.setColorFilter( new ColorMatrixColorFilter(BT_SELECTED) ) ;
    }else{
     v.getBackground().setColorFilter( new ColorMatrixColorFilter(BT_SELECTED) );
     v.setBackgroundDrawable(v.getBackground());
    }
   } else if (event.getAction() == MotionEvent.ACTION_UP) {
    if(v instanceof ImageView){
     ImageView iv = (ImageView) v;
     iv.setColorFilter( new ColorMatrixColorFilter(BT_NOT_SELECTED) ) ;
    }else{
     v.getBackground().setColorFilter(
       new ColorMatrixColorFilter(BT_NOT_SELECTED));
     v.setBackgroundDrawable(v.getBackground());
    }
   }
   return false;
  }
 };
 
 /**
  * 让控件点击时,颜色变暗
  * */
 public static final OnTouchListener VIEW_TOUCH_LIGHT = new OnTouchListener(){

  public final float[] BT_SELECTED = new float[] { 1, 0, 0, 0, 50, 0, 1,
    0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0 };
  public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
  
  @Override
  public boolean onTouch(View v, MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
    if(v instanceof ImageView){
     ImageView iv = (ImageView) v;
     iv.setDrawingCacheEnabled(true);
     
     iv.setColorFilter( new ColorMatrixColorFilter(BT_SELECTED) ) ;
    }else{
     v.getBackground().setColorFilter( new ColorMatrixColorFilter(BT_SELECTED) );
     v.setBackgroundDrawable(v.getBackground());
    }
   } else if (event.getAction() == MotionEvent.ACTION_UP) {
    if(v instanceof ImageView){
     ImageView iv = (ImageView) v;
     iv.setColorFilter( new ColorMatrixColorFilter(BT_NOT_SELECTED) ) ;
     System.out.println( "变回来" );
    }else{
     v.getBackground().setColorFilter(
       new ColorMatrixColorFilter(BT_NOT_SELECTED));
     v.setBackgroundDrawable(v.getBackground());
    }
   }
   return false;
  }
 };
}

颜色地址: http://www.ioracle.org/attachment/ColorMatrixDemo.swf
该方法原文:http://blog.csdn.net/hfmbook/article/details/17413351

3、Android开发中,View的图片是动态生成的,我们需要增加点击效果。

iReader 的书架中的书籍,点击变暗

使用很简单:

    ImageView iv = (ImageView) this.findViewById(R.id.image_view1);

    Drawable d = Drawable.createFromPath("picPath");

        StateListDrawable sld = createSLD(this, d);
        iv.setImageDrawable(sld);


///增加如下两个方法:

public Drawable createDrawable(Drawable d, Paint p) {

        BitmapDrawable bd = (BitmapDrawable) d;
        Bitmap b = bd.getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(bd.getIntrinsicWidth(),
                bd.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(b, 0, 0, p); // 关键代码,使用新的Paint画原图,

        return new BitmapDrawable(bitmap);
    }


    /** 设置Selector。 本次只增加点击变暗的效果,注释的代码为更多的效果*/ 
    public StateListDrawable createSLD(Context context, Drawable drawable) {
        StateListDrawable bg = new StateListDrawable();
        Paint p = new Paint();
        p.setColor(0x40222222); //Paint ARGB色值,A = 0x40 不透明。RGB222222 暗色


        Drawable normal = drawable;
        Drawable pressed = createDrawable(drawable, p);
        // p = new Paint();
        // p.setColor(0x8000FF00);
        // Drawable focused = createDrawable(drawable, p);
        // p = new Paint();
        // p.setColor(0x800000FF);
        // Drawable unable = createDrawable(drawable, p);
        // View.PRESSED_ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled }, pressed);
        // View.ENABLED_FOCUSED_STATE_SET
        // bg.addState(new int[] { android.R.attr.state_enabled,
        // android.R.attr.state_focused }, focused);
        // View.ENABLED_STATE_SET
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);
        // View.FOCUSED_STATE_SET
        // bg.addState(new int[] { android.R.attr.state_focused }, focused);
        // // View.WINDOW_FOCUSED_STATE_SET
        // bg.addState(new int[] { android.R.attr.state_window_focused },
        // unable);
        // View.EMPTY_STATE_SET
        bg.addState(new int[] {}, normal);
        return bg;
    }

http://www.educity.cn/wenda/169695.html

0 0
原创粉丝点击