我正在尝试创建的是一个可缩放和可滚动的ImageView,它也可以接受大图像。目前我在我的覆盖自定义ImageView中有这个方法:
public void setLargeImageFromResource(int imageResource){
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 10;
Bitmap bitm =
BitmapFactory.decodeResource(getContext().getResources(),
imageResource, options);
super.setImageBitmap(bitm);
}
我在构造函数中调用它是这样的:
public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TouchImageView, defStyle, 0);
int base = a.getResourceId(R.styleable.TouchImageView_imageDrawable, R.drawable.kaart);
setLargeImageFromResource(base);
}
我的布局XML如下所示:
<nl.raakict.android.util.TouchImageView
android:id="@+id/plattegrond"
android:layout_width="match_parent"
android:layout_height="match_parent"
imageDrawable="@drawable/kaart"
/>
我的attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TouchImageView">
<attr name="imageDrawable" format="string"/>
</declare-styleable>
</resources>