Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Android)How can I show a part of image?

Tags:

I have an image like this->

enter image description here

And I want to show like this->

enter image description here

Then I can choose how much I can see(0~1,0=can not see,1=Whole picture,0.5=half picture,....etc,read from user input)

How to do that?

I tried to use android:scaleType but it is not working.

like image 390
范植勝 Avatar asked Aug 06 '13 06:08

范植勝


People also ask

What tool do we use to display images in Android?

ImageView class is used to display any kind of image resource in the android application either it can be android.

What is Drawable in Android Studio?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.


1 Answers

My best suggestion is to wrap your BitmapDrawable with a ClipDrawable.

ClipDrawable lets you define clipping for any other drawable, so instead of drawing the entire drawable, only a part of it will be drawn.

How would that work? Your ImageView can display a drawable - assignable through setImageDrawable(). Naturally, you would place a BitmapDrawable of your image bitmap there. If you wrap your BitmapDrawable first with ClipDrawable, and only then assign to the ImageView, you should be fine.

<ImageView         android:id="@+id/imageView1"         android:layout_width="100dp"         android:layout_height="100dp"         android:src="@drawable/clip_source" /> 

and this is clip_source drawable:

<?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" /> 

You can define the amount of clipping by calling the function setLevel() on your ClipDrawable (clip_source). A level of 0 means the image is completely hidden and a level of 10000 means the image is completely revealed. You can use any int value in the middle.

You'll have to set the level in code, so your code should first get a reference to the ClipDrawable. You can do this by running getDrawable() on your ImageView instance. When you have a reference to your ClipDrawable, simply run setLevel(5000) on it (or any other number 0-10000).

ImageView img = (ImageView) findViewById(R.id.imageView1); mImageDrawable = (ClipDrawable) img.getDrawable(); mImageDrawable.setLevel(5000); 
like image 96
talkol Avatar answered Sep 19 '22 14:09

talkol