5

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.

Falko
  • 17,076
  • 13
  • 60
  • 105
Akanksha Rathore
  • 3,603
  • 3
  • 31
  • 47

3 Answers3

3

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.

Akanksha Rathore
  • 3,603
  • 3
  • 31
  • 47
0

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
Carlos Robles
  • 10,828
  • 3
  • 41
  • 60
  • My shape is closed. otherwise it will throw error. that is not an issue. and i can't set shape in imageView's xml because I need to draw it and remove it on runtime. border is only for image selection. If user select another imageview then border view will remove from first and set it on another view. – Akanksha Rathore Dec 09 '13 at 05:10
  • I update the answer.If you need to change it in runtime, i guess that adding and removing the background will do it. please try that and let mi know what happens. – Carlos Robles Dec 09 '13 at 08:51
  • I can't use this also.look I have two images.In case i am showing border on imageview my another image is also there which has to look always. only border will be show and hide. If set null in background my both image will get deleted. – Akanksha Rathore Dec 09 '13 at 09:06
  • I'm not sure i understand what you say, specially with this of your images being deleted. Anyways I guess i lack much code to be able to make a good appreciation. probably you should post more code to show us how are your images in the xml, and what other operations are you performing to compose how it will be displayed – Carlos Robles Dec 09 '13 at 09:11
  • see the photo fancie app u will understand how they are drawing border on image selection. – Akanksha Rathore Dec 09 '13 at 09:33
  • can you give me a link? anyways i think i understand what you want, but i dont how how your current elements are. – Carlos Robles Dec 09 '13 at 10:09
  • https://play.google.com/store/apps/details?id=com.surmin.photofancie.lite this is the link of app try Edit image. u will see border draw concept there. – Akanksha Rathore Dec 09 '13 at 11:11
  • I got it. I think you can use a relative layout for each picture. Inside, you put all the images or views you need, that will act as layers. And you apply the border to the relative layout (with setBackgroundDrawable). No matter what you do with the relative layout background, the images will have its independency. so you can add or remove the border without messing with the images. Also, if you want, you can nest relative layouts if it easier to you to move related layers. – Carlos Robles Dec 09 '13 at 12:10
  • I have so many types of collage design. I don't want to create separate layout for each imageView. – Akanksha Rathore Dec 09 '13 at 13:06
  • I understand. Probably at this point the best for you is to create your own view type, extending ImageView, and overriding `onDraw` and maybe `onMeasure` – Carlos Robles Dec 09 '13 at 14:11
0

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.

Community
  • 1
  • 1
Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26