0
我正在擴展ImageView以手動縮放視圖內的圖像。我想縮放圖像以填充自定義視圖的寬度,然後將其繪製到畫布上,但是,我無法使用this.getWidth()
它只返回0,因爲視圖尚未繪製等具有0.自定義ImageView內縮放圖像
目前尺寸0在我main.xml中我有以下幾點:
<com.android.myapp.BackgroundView
android:id="@+id/background_view"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/background"
android:dither="true"
/>
自定義類如下:
public class BackgroundView extends ImageView {
private Paint paint;
private Bitmap background;
public BackgroundView(Context context) {
super(context);
paint = new Paint();
loadBitmap();
}
public BackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
loadBitmap();
}
public void loadBitmap() {
BitmapDrawable src = (BitmapDrawable) this.getDrawable();
background = src.getBitmap();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(background, 0, 0, paint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
我Main.java類:
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
我不能使用
Bitmap.createScaledBitmap(background, view.getWidth(), view.getHeight(), false)
的觀點尚未制定,我怎麼會去縮放圖像,以填補在這一點上的看法/屏幕寬度?
在此先感謝。