[英](Android)How can I show a part of image?
我有這樣的圖像->
我想這樣展示->
然后我可以選擇我能看到多少(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);
這是實現這一目標的另一種方法
public static Bitmap getScaleBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()/2);
final RectF rectF = new RectF(rect);
final float roundPx = 0;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
在下面的行中
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()/2);
根據需要設置高度..
享受 :)
我通過在標記中在我的 ImageView 上設置 scrollY 來實現這一點。 從 android 的文檔中,如果您正在處理運行低於 API 14 的設備,顯然這不是一個選項。此外,在我的情況下,我使用的是基於應用程序狀態加載的固定大小的圖像,因此它們是總是一樣的大小。 因此,我可以在標記中實現它。 我意識到這種方法並不適用於所有人。
我將圖像包裝在一個與圖標一樣高的布局中,並有意將圖標的大小設為實際大小。 如果沒有設置 scrollY,它只顯示圖像的上半部分。 設置 scrollY 將其移動到我想要的位置 - 僅顯示圖像的下半部分。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/default_icon"
android:scrollY="50dp" />
</LinearLayout>
所以,從我的例子來看,它只顯示圖標的下半部分。
這是一個簡單的方法.. 在您的主布局中為您的圖像創建一個單獨的布局。 確保它是真實的。 現在將您的圖片的圖像視圖放在布局旁邊並使其填充父級。 OK 現在制作一個空白圖像並將該圖像視圖添加到布局的底部,將頂部添加到布局的中心。 只需弄亂圖像視圖的邊距和大小,直到它看起來像你想要的那樣。 希望這可以幫助。
將您的 drawable 轉換為位圖並裁剪以獲取圖像的頂部以形成另一個位圖,然后將其顯示到您的圖像視圖中。
// to convert drawable to bitmap
Bitmap resource = BitmapFactory.decodeResource(context.getResources(),
R.drawable.your_resource);
// to set width of bitmap to imageview's width if necessary,
int width = imageView.getMeasuredWidth();
// to crop the bitmap to specific width and height,
resource = Bitmap.createBitmap(resource, 0, 0, width, height);
這是將圖像裁剪成兩部分的代碼
public static Bitmap getScaleBitmap(Bitmap bitmap, int check) {
final Bitmap toBeCropped = bitmap;
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inTargetDensity = 1;
toBeCropped.setDensity(Bitmap.DENSITY_NONE);
int top = 0;
int bottom = 0;
int targetheight = 0;
if (check == 0) {// return 1st half of image
top = 0;
bottom = bitmap.getHeight() / 2;
} else {// return 2nd half of image
top = (bitmap.getHeight() / 2) - 10;
bottom = (bitmap.getHeight() / 2) - 10;
}
int fromHere = (int) (toBeCropped.getHeight() * 0.5);
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, top, toBeCropped.getWidth(), bottom);
return croppedBitmap;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.