2013-08-06 76 views
16

我喜歡這個 - >(Android)如何顯示圖像的一部分?

enter image description here

的形象和我想告訴喜歡這個 - >

enter image description here

然後我可以選擇我多少可以看到(0〜1 ,0 =看不見,1 =全圖,0.5 =半圖,....等,從用戶輸入讀取)

如何做到這一點?

我試過使用android:scaleType,但它不起作用。

+0

http://stackoverflow.com/questions/14544327/make-image-appear-half-of-the-screen – Rohit

回答

19

我最好的建議是把你的BitmapDrawable換成ClipDrawable

ClipDrawable允許您爲任何其他drawable定義剪裁,所以不是繪製整個drawable,只繪製其中的一部分。

這將如何工作?您的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" /> 

您可以通過調用函數setLevel()您ClipDrawable(clip_source)定義了限幅量。 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); 
+0

如何setLevel? PLZ定義更多關於它的信息。 –

+2

新增說明 – talkol

+0

太棒了。非常感謝。 – Neela

0

這是一個簡單的方法..在您的主要佈局創建一個單獨的佈局,只爲您的圖像。確保它是真實的。現在將您的圖片視圖放置在佈局的側面,並使其填充父項。好吧,現在製作一張空白圖片,並將該圖片視圖添加到佈局的底部,並將頂部添加到佈局的中心。只需將圖像瀏覽的邊距和尺寸弄亂,直到看起來像你想要的那樣。希望這可以幫助。

+0

第二個inageview可以只是白色的例子。只是爲了隱藏主圖像視圖的下半部分。 – NightSkyCode

2

這裏是另一種方式在下面一行來實現這一

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

設置HIGHT根據您的需要..

享受:)

+0

如何實施中心作物。 –

+0

你是指圓形還是長方形? –

+0

剪輯不適合我,但是這樣做:) – Makalele

0

我已經通過在標記中將ImageY設置爲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> 

所以,從我的例子來看,它只顯示圖標的下半部分。

0

將您的繪圖轉換爲位圖並剪裁以獲取圖像的頂部以形成另一個位圖,然後將其顯示到圖像視圖中。

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