我有这样的图像->
我想这样表现->
然后我可以选择我能看到多少(0~1,0=看不到,1=全图,0.5=半图,....等,从用户输入读取)
怎么做?
我尝试使用 android:scaleType
但它不起作用。
最佳答案
我最好的建议是把你的 BitmapDrawable 包起来。与 ClipDrawable .
ClipDrawable 允许您为任何其他可绘制对象定义剪辑,因此不会绘制整个可绘制对象,而只会绘制其中的一部分。
这将如何工作?您的 ImageView 可以显示可通过 setImageDrawable()
分配的可绘制对象。自然地,您会在此处放置图像位图的 BitmapDrawable。如果你先用 ClipDrawable 包装你的 BitmapDrawable,然后再分配给 ImageView,你应该没问题。
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/clip_source" />
这是 clip_source
可绘制的:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="@drawable/your_own_drawable"
android:gravity="top" />
您可以通过调用 ClipDrawable (clip_source
) 上的函数 setLevel()
来定义剪辑量。级别 0 表示图像完全隐藏,级别 10000 表示图像完全显示。您可以在中间使用任何 int 值。
您必须在代码中设置关卡,因此您的代码应该首先获得对 ClipDrawable 的引用。您可以通过在 ImageView 实例上运行 getDrawable()
来做到这一点。当您有对 ClipDrawable 的引用时,只需在其上运行 setLevel(5000)
(或任何其他数字 0-10000)。
ImageView img = (ImageView) findViewById(R.id.imageView1);
mImageDrawable = (ClipDrawable) img.getDrawable();
mImageDrawable.setLevel(5000);
关于android - (Android)如何显示图像的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18073588/