How To Scale The Image Color According To Value
How to fill the image according to percentage of TextView. and it should change according to percentage of TextView .in the following code the height layout is changing but i want
Solution 1:
You need to use a Clip drawable.
See here :
http://developer.android.com/guide/topics/resources/drawable-resource.html#Clip
For instance in your code :
privatevoiddisplayData(String data) {
if (data != null) {
battery.setText(data);
int x=Integer.parseInt(battery.getText().toString());
ClipDrawable drawable = (ClipDrawable) image_level.getDrawable();
drawable.setLevel(100 * x);
}
}
In your layout :
<ImageView
android:id="@+id/image_level"
android:background="@android:color/white"
android:src="@drawable/clip"
android:layout_height="50dp"
android:layout_width="match_parent" />
And for your drawable/clip.xml :
<?xml version="1.0" encoding="utf-8"?><clipxmlns:android="http://schemas.android.com/apk/res/android"android:drawable="@color/myBlueColor"android:clipOrientation="horizontal"android:gravity="left" />
And for your myBlueColor in colors.xml :
<colorname="myBlueColor">#0383f3</color>
Solution 2:
the following my method to get the battery percentage and pass that value to imagelevel button to display level.
private void displayData(String data) {
Log.v("______________________No serives_______________",data );
if (data != null) {
battery.setText(data);
int x=Integer.parseInt(battery.getText().toString());
image_level.getLayoutParams().height = x*2;
Log.v("______BATERY LEVEL_________", "__IN DISPLAYDATA()___DCA__________"+data);
}
and the following code is in layout to display.
<RelativeLayout
android:id="@+id/image_layout"
android:layout_width="100dp"android:layout_height="100dp"android:layout_below="@+id/linearlayout"
android:background="@drawable/ic_launcher"
>
<Button
android:id="@+id/image_level"android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#8099FFFF"
android:contentDescription="@string/app_name"/>
Solution 3:
Create layer-list in your drawable folder like this
doubleshade.xml
<itemandroid:drawable="@color/white"/><itemandroid:right="60dp"><shapeandroid:shape="rectangle" ><solidandroid:color="@color/blue" /></shape></item>
In your code apply this shape according to the condition
ImageViewimg= (ImageView) findViewById(R.id.imageView);
TextViewtextview= (TextView) findViewById(R.id.textView);
intper= Integer.parseInt(textview.getText().toString());
if(per >= 60 )
{
img.setImageResource(R.drawable.doubleshade);
}
Post a Comment for "How To Scale The Image Color According To Value"