Question

This is my shape_two file which i put in my drawable folder.

<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#00FFFFFF" />

<stroke
    android:width="3dp"
    android:color="#00ff00" />

</shape>

this is my code to draw shape

private void drawLayer(ImageView tempView, Drawable drble, int wherecall) {

    Drawable drawableRes = getResources()
            .getDrawable(R.drawable.shape_two);// loadDrawable(R.drawable.shape_two);
            int h = tempView.getMeasuredHeight();
    int w = tempView.getMeasuredWidth();
    drawableRes.setBounds(0, 0, w, h);
    Drawable[] drawableLayers = { drble, drawableRes };
    ld = new LayerDrawable(drawableLayers);
    if (wherecall != 3) {
        tempView.setBackground(ld);
    } else {
        tempView.setImageDrawable(ld);
    }
    td = new TransitionDrawable(drawableLayers);
    td.setCrossFadeEnabled(true);

}

here tempview is my ImageView and drble is another drawable which i am using on another layer. This code don't have any error message working fine.

My problem is when i draw shape on my ImageViews it get re-size(change height and width). i have different size of imageView and i want to use single shape xml to draw border and my ImageView size should not change while doing so. How to achieve it can anyone tell me.

After searching to many things i came to know i can't perform bitmap operations on it to re-size it according to shape because xml file is not a bitmap. I read somewhere that this issue can be resolve by using canvas but how do it..

UPDATE:

I observe that while change layer onsingletap only height is increasing not width of the ImageView. If my Layout have 4 ImageView so on click first time only on different imageView, All imageView re-size once. After that if we click again-again nothing get re-size. I think on setting layer on ImageView re-size view for fist time, then it adjusted itself.I don't know why is this happening.

I am applying same code for my all layout and for some layout its working fine and for some its not.

What i have tried yet.

  1. I have tried make border image by GradientDrawable

     GradientDrawable gd = new GradientDrawable();
         gd.setStroke(3, Color.GREEN);
         gd.setSize(drble.getBounds().width(), drble.getBounds().height());
         gd.setColor(0xff424242);
    
  2. My another try was draw image on canvas,

  3. put border image in drawable folder re-size it according to ImageView height and width convert it in drawable and set it.
  4. I also tried to reduce Inner image size and border bitmap size is size of Image view and opposite of it also.

Every thing give me same result.

UPDATE

Please go through my answer, you may found good point to search and get +50.

Was it helpful?

Solution

After struggled a lot, used so many ways and spending a day and night in searching finally i found my solution in these three lines.

    ld.setBounds(0, 0, tempView.getWidth(), tempView.getHeight());
    ld.setLayerInset(0, 0, 1, 0, 1);
    ld.setLayerInset(1, 0, 0, 0, 0);

If still you have problem of re-sizing imageView give some padding in your shape xml layout.This may save someones time.

OTHER TIPS

Why you are using that function to draw the border? have you tried to assign it directly in the layout?

<ImageView 
 android:background="@drawable/shape_two"
...
/>

UPDATE

If you need to add or remove the border dynamically, you can use

tempView.setBackgroundDrawable(R.drawable.shape_two);//to add it
tempView.setBackgroundDrawable(null);//to remove it

Please take a look at this solution link

You need to add a frame laout and create an ImageView on it, and do your border operation on the frame layout.

Also keep some margin to your image view, so that background framelayout is visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top