1

i tried to add an own drawable object to an ImageView. i want that the drawable fits the ImageView. But if my resolution is to small the drawable is bigger than the ImageView.

Here my ImagaView:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>    
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
           ...
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dip"
                android:id="@+id/tvWeekStats"
                android:layout_below="@+id/llTimer"
                android:layout_margin="5dip"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
             />
      </RelativeLayout>
  </ScrollView>

And here is my drawable:

public class WeekdayDrawable extends Drawable {

private Paint paint;

public WeekdayDrawable() {
    this(new StatsWeekday());
}

public WeekdayDrawable(StatsWeekday ownTeam) {        
    paint = new Paint();
    paint.setStyle(Style.FILL);
}

@Override
public void draw(Canvas canvas) {
    try {
        canvas.save();

        paint.setColor(Color.WHITE);
        canvas.drawLine(0, 0, 700, 0, paint);
        canvas.drawLine(0, 10, 600, 10, paint);
        canvas.drawLine(0, 20, 500, 20, paint);
        canvas.drawLine(0, 30, 400, 30, paint);
        canvas.drawLine(0, 40, 300, 40, paint);

        canvas.drawRect(33, 60, 77, 77, paint );
        paint.setColor(Color.YELLOW);
        canvas.drawRect(33, 33, 77, 60, paint );            
        paint.setColor(Color.WHITE);
        paint.setTextSize(20);

        canvas.drawText("Some Text", 10, 25, paint);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void setAlpha(int alpha) {
    // Has no effect
}

@Override
public void setColorFilter(ColorFilter cf) {
    // Has no effect
}

@Override
public int getOpacity() {
    // Not Implemented
    return 0;
}

}

        Drawable weekStats;
        weekStats = new WeekdayDrawable(weekDayData);


        tvWeekStats.setImageDrawable(weekStats);

Thanks for any help

mmBs
  • 8,421
  • 6
  • 38
  • 46
BHuelse
  • 2,889
  • 3
  • 30
  • 41

2 Answers2

1

1) Make sure your ImageView has these attributes:

android:adjustViewBounds="true"
android:scaleType="centerInside"

This should center the image and fit it within your imageview.

2) For some apparent reason drawables like that wont be subject to the attributes. It appears that it must be a bitmap or imageResource for the attributes to affect the image. In other words CONVERT your drawable into a bitmap. Here is a pre-written code which I took from: https://stackoverflow.com/a/10600736/1371041

    public static Bitmap drawableToBitmap (Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable)drawable).getBitmap();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap; 
    }

3) Use the method to convert it into a bitmap and set it using this:

 myImageView.setImageBitmap(drawableToBitmap(weekStats));
Community
  • 1
  • 1
ChallengeAccepted
  • 1,684
  • 1
  • 16
  • 25
  • This doesn't change anything. Same problem, my drawable is bigger than ImageView and isn't shown complete. – BHuelse Oct 18 '13 at 09:10
  • I had a similar problem to this and resolved it. I'm on my phone now and going to bed. If it isn't resolved by tomorrow morning I'll post my code. I'm very positive it is related to the XML file and not your java file. I don't have my computer at the moment else I would have posted the attributes for the XML file. – ChallengeAccepted Oct 18 '13 at 09:14
  • I have added the parentsview from my ImageView. May it helps. – BHuelse Oct 18 '13 at 13:08
  • I have updated the answer, check it out. If it fixed your problem please select the checkmark near my solution to indicate it was the answer. Hope it worked out! :) – ChallengeAccepted Oct 18 '13 at 17:39
  • I'm sorry then. Not sure how to solve it. There is although always a solution with programming. Try placing a bounty when your high enough of a level to attract more answers. I upped your question so you get points and for others to notice it. – ChallengeAccepted Oct 25 '13 at 07:40
  • I found some similar posts. Check these out for their solutions: http://stackoverflow.com/questions/9984360/how-to-set-imageview-drawable-size-based-on-its-parent-view-size – ChallengeAccepted Oct 25 '13 at 07:46
1

You shouldn't use static width and height like this canvas.drawLine(0, 0, 700, 0, paint);. Use Fractions of getHeight() and getWidth() instead, like:

canvas.drawLine(0, 0, getHeight()/2, 0, paint);

or so. Then the scalingproblem solves itself

chuck258
  • 912
  • 7
  • 16
  • I hope the ImageView can handle it for me. My drawable is much bigger as i post here. But it seems i have to do this work. – BHuelse Oct 18 '13 at 14:08