Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

how to fill the color of image according to percentage of textview in android

How to fill the image according to percentage of textview.and it should change according to percentage of textview.below is my code so please check.

              private void displayData(String data) {
    if (data != null) { 
        battery.setText(data);
    }
    }

this is my textview i am getting data as 100 or some 60 like that.what ever value i got that much level my imageview should fill with color and display.

             <TextView  android:id="@+id/batterylevel"
android:layout_width="wrap_content"
android:layout_height="150dp" android:text="BRV Battery"  android:layout_below="@+id/sos"/>

  <ImageView 
      android:id="@+id/image_level"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" 
android:layout_toRightOf="@+id/batterylevel"
android:layout_below="@+id/sos"
android:layout_above="@+id/portbearmode"

android:alpha="1"
android:adjustViewBounds="true"

 android:contentDescription="@string/app_name"/>

in the layout i did like this i should apply any animations or simply setting any options please tell me. if battery textview is 60 it should fill blue color upto 60 and remaining 40 as white color.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if u want to change the opacity then you can simply set the opacity according to the value in textview usinf setalpha method.

I have a good idea to solve this simply try it your self.

Put your imageview in a linear/relative layout set the heightto the max width that u want to assign for the image view.

Then initially set the heightof the image view to zero

Then depending on the value from textview set the height of the image view programatically

 int x=Integer.parseInt(battery.getText().toString());

 image_level.getLayoutParams().height = x;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...