1

I am setting a layer list to an imageview with drawable programatically in android

My custom Drawable is as follows,

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;
}

}

This is my code to create layer list drawable and set it to 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) {
    }
}

and on click of imageview, i am trying to change the drawable but failed as the condition is working perfectly but layerlist drawable is not changing,

My code on click listener of 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;
Reprator
  • 2,859
  • 2
  • 32
  • 55
  • I guess you are searching something like a selector or a LevelListDrawable? You want to switch the image by click, aren´t you? I´m not toally familiar with that what your trying, but I guess You need a `LevelListDrawable` or you have to create a selector..... – Opiatefuchs Apr 19 '16 at 11:03
  • yes sure, but i wanted to do this with above mentioned code not with xml or any other code. – Reprator Apr 19 '16 at 11:12
  • You can do a selector programmatically, I think. look here: http://stackoverflow.com/questions/32226232/android-create-selector-programmatically. I think imageView.setImageLevel just works with a LevelListDrawable. – Opiatefuchs Apr 19 '16 at 11:22
  • No, i don't wanted a selector, i wanted to change the whole drawable and make it permanent on each click of imageview. – Reprator Apr 19 '16 at 11:24
  • so , why not just set the image with imageView.setImageResource(R.drawable.yourdrawable) or imageView.setImageBitmap(bitmap) ? – Opiatefuchs Apr 19 '16 at 11:26
  • i know it but i wanted to do this way, – Reprator Apr 19 '16 at 11:27
  • So, it´s not possible that way. A LayerDrawable overlays the images one over another. As described in API: `These are drawn in array order, so the element with the largest index will be drawn on top` . And the API desription for `setImageLevel()` is :`Sets the image level, when it is constructed from a LevelListDrawable` . This points out that a LevelListDrawable has to be used. So, I´m sorry to tell you that what you want to do is impossible. – Opiatefuchs Apr 19 '16 at 11:53
  • @Opiatefuchs, i got it just forget to use levelist drawable in place of layer drawable and now it is working fine. – Reprator Apr 19 '16 at 11:56
  • and thanks for your patience and help.... can u please help me in setting border to this custom drawable?? – Reprator Apr 19 '16 at 12:01

0 Answers0