单击时,使用imageview不会更改layerlist drawable

时间:2016-04-19 10:50:49

标签: android android-drawable layer-list layerdrawable android-custom-drawable

我正在使用android

中的可编程方式将图层列表设置为imageview

我的自定义Drawable如下,

package cl.tk.ui.iBAPView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import cl.tk.R;

public class CircleDrawable extends Drawable {

private int color;
private boolean checked = false;
private int size;
private Paint paint= new Paint(Paint.DITHER_FLAG);
private Context context;

public CircleDrawable(Context context,boolean checked,int color,int size) {
    this.checked=checked;
    this.color=color;
    this.size=size;
    this.context=context;
}

@Override
public void draw(Canvas canvas) {
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);
    canvas.drawCircle(size/ 2, size / 2,size / 2, paint);

    if (checked)
        drawChecked(canvas);
}

private void drawChecked(Canvas canvas) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_check);

    float posX = (canvas.getWidth()  - bitmap.getWidth()) / 2;
    float posY = (canvas.getHeight() - bitmap.getHeight()) / 2;
    canvas.drawBitmap(bitmap, posX, posY, paint);
}

@Override
public void setAlpha(int alpha) {
    // do nothing
}

@Override
public void setColorFilter(ColorFilter cf) {
    // do nothing
}

@Override
public int getOpacity() {
    return PixelFormat.UNKNOWN;
}

}

这是我创建图层列表drawable并将其设置为imageview的代码,

  public static void setChecked_Selector(Context context,ImageView view) {
    try {
        int size= (int) context.getResources().getDimension(R.dimen._17sdp);
       /* Drawable pressed=squareView(ContextCompat.getColor(context,R.color.colorBlue),ContextCompat.getColor(context,R.color.colorRed),size);//new BadgeDrawable(context,colorPressed);
        Drawable normal=squareView(ContextCompat.getColor(context,R.color.colorwhite),ContextCompat.getColor(context,R.color.colorRed),size);*/
        Drawable pressed=new CircleDrawable(context,true,ContextCompat.getColor(context,R.color.colorGreen),size);
        Drawable normal=new CircleDrawable(context,false,ContextCompat.getColor(context,R.color.colorGray),size);

        Drawable[] drawarray = {pressed, normal};
        LayerDrawable layerdrawable = new LayerDrawable(drawarray);
        view.setBackgroundDrawable(layerdrawable);
        view.setImageLevel(0);
        view.setTag(0);
    } catch (Exception e) {
    }
}

并且点击了imageview,我正在尝试更改drawable但是因为条件工作正常而失败但是layerlist drawable没有改变,

我在imageView的点击监听器上的代码,

case R.id.ah_img_agree:
        {
            int imageLevel=-1;
            if(0==(int)v.getTag())
                imageLevel=1;
            else
                imageLevel=0;
            ((ImageView)v).setImageLevel(imageLevel);
            rbAgreed.setTag(imageLevel);
        }
        break;

0 个答案:

没有答案