如何根据TextView的在Android的比例填充图像的颜色图像、比例、根据、颜色

2023-09-13 23:59:27 作者:皒会好好珍惜

如何根据textview.and应根据textview.below的百分比变化的百分比填写的形象是我的code所以请检查。

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

这是我的TextView我得到的数据是100或约60类似that.what以往任何时候都值我有那么多的水平我的ImageView应填充颜色和显示。

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

在布局我不喜欢这样,我应该适用于任何动画或简单地设置任何选项,请告诉我。 如果电池的TextView是60应该填充蓝色高达60和其余的40为白色。

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.

推荐答案

以下我的方法来获得电池百分比和传递价值的映像级别按钮显示的水平。

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

和以下code是在布局显示。

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