1

我试图将自己的可绘制对象添加到 ImageView。我希望drawable适合ImageView。但如果我的分辨率太小,drawable 就会比 ImageView 大。

这是我的 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>

这是我的可绘制对象:

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

谢谢你的帮助

4

2 回答 2

1

你不应该像这样使用静态宽度和高度canvas.drawLine(0, 0, 700, 0, paint);。改用 getHeight() 和 getWidth() 的分数,例如:

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

或者。然后缩放问题自行解决

于 2013-10-18T13:35:40.373 回答
1

1) 确保您的 ImageView 具有以下属性:

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

这应该使图像居中并将其放入您的图像视图中。

2)由于某些明显的原因,像这样的drawables不会受到属性的影响。看来它必须是位图或 imageResource 属性才能影响图像。换句话说,将您的可绘制对象转换为位图。这是我从中获取的预先编写的代码: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)使用方法将其转换为位图并使用以下设置:

 myImageView.setImageBitmap(drawableToBitmap(weekStats));
于 2013-10-18T09:03:24.020 回答