刷新ImageView

时间:2013-04-04 20:05:27

标签: android imageview batterylevel

我想创建一个Android应用程序,在textView中给出电池状态(%)。 我还有一个ImageView,其中包含我想根据电池状态值更改的图像。 (我有不同的电池图像对应不同的电荷水平)。

我已经可以在textView中打印电池状态,但我不知道如何更改图像视图中的图像。这是我的主要片段:

public class Battery extends Fragment{ 

private TextView contentTxt;
private ImageView battery; 
int level;
int scale;
int test;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
           Bundle savedInstanceState) {
          View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
          contentTxt=(TextView) myFragmentView.findViewById(R.id.monospaceTxt);

          getActivity().registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
          return myFragmentView;

        }

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            int level = intent.getIntExtra("level", 0);
            int scale = intent.getIntExtra("scale", 100);
            contentTxt.setText(String.valueOf(level * 100 / scale) + "%");


        }
};


}

BatteryService来源:

public class BatteryService extends Service {
    private int APP_ID = 10000;
    private int currentPercent = 0;
    private Intent intentFrom;
    public static boolean isRunning;

    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    int level = intent.getIntExtra("level", 0);
        int scale = intent.getIntExtra("scale", 100);
        Log.d("BATTERY_STAT", intent.getClass().getName());

        currentPercent = level * 100 / scale;
                    //Notification notification = new Notification(R.drawable.icon,
                            //context.getResources().getString(R.string.your_battery_status), System.currentTimeMillis());
                   // notification.number = currentPercent;
                   // notification.flags = Notification.FLAG_NO_CLEAR; 

                   // Intent contentIntent = new Intent(context,Main.class);          
                   // notification.setLatestEventInfo(context,
                                  //  context.getResources().getString(R.string.battery_status) + " " + String.valueOf(notification.number) + "%",
                                  //  context.getResources().getString(R.string.your_battery_status),
                      //  PendingIntent.getActivity(context, 0, contentIntent,Intent.FLAG_ACTIVITY_NEW_TASK));

                  //  mManager.notify(APP_ID , notification);

            }
    };
    private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
    }

    @Override
    public void onCreate() {
            // TODO Auto-generated method stub
            super.onCreate();
            Log.d("BATTERY_STAT", "battery service onCreate");
            BatteryService.isRunning = true;
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    public void onDestroy() {
            // TODO Auto-generated method stub
            this.unregisterReceiver(this.mBatInfoReceiver);
            BatteryService.isRunning = false;
            removeServiceNotification();
            super.onDestroy();

    }

    @Override
    public void onStart(Intent intent, int startId) {
            // TODO Auto-generated method stub
            intentFrom = intent;
            super.onStart(intent, startId);
    }

    private int getPercentBatt(){
            return currentPercent;
    }


    private final IBattery.Stub binder=new IBattery.Stub() {
               public int getPercent() {
                 return(getPercentBatt());
               }
               public void stopNotification(){
                       removeServiceNotification();
               }
    };

    public  void removeServiceNotification(){
            mManager.cancel(APP_ID);
    }

}

和我的xml文件:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/fond_correct" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="  Battery Status"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_marginTop="80px"
    android:layout_marginLeft="10px"
    android:layout_gravity="center"
    android:textStyle="bold"
    android:textSize="30sp"

     />


<TextView
    android:id="@+id/monospaceTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="100%"
    android:textSize="50px"
    android:typeface="monospace"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="35px" />

<ImageView
    android:id="@+id/battery"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:scaleType="center"
   android:src="@drawable/battery1"
   android:layout_gravity="center_horizontal"/>



</LinearLayout>

在私有的BroadcastReceiver中,mBatInfoReceive我尝试了一个“if”条件,但它没有用,soplease,如果你可以帮我处理这个问题。

谢谢

2 个答案:

答案 0 :(得分:1)

也许我只是不明白你的问题。您的问题是您不知道如何以编程方式更改imageview中的图像?您可以使用setImageResource(resourceId),在您的情况下使用电池。 setImageResource(RESOURCEID)。我想你对不同的电池电量有不同的图像。

答案 1 :(得分:0)

您应该将此添加到onCreateView:

battery=(ImageView) myFragmentView.findViewById(R.id.battery);

这是onReceive:

battery.setImageResource(R.drawable.battery1);