【发布时间】:2015-08-27 01:01:59
【问题描述】:
在我的应用程序中,操作栏上有一个图标,当我单击某些内容开始同步时,我想将图标更改为 gif,当同步结束时再次更改它
我试过了,但应用程序崩溃了:
MenuItem menuItem = (MenuItem)findViewById(R.id.update_data); ((Menu) menuItem).getItem(0).setIcon(getResources().getDrawable(R.drawable.space));
这是我的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.update_data:
Log.i("TAG", "on update data");
if(Globals.onFitnessTutorial){
Globals.onFitnessTutorial = false;
finish();
CommonUtils.startIntent(this, ProgressActivity.class);
}
if (MyApplication.isBluetoothConnection) {
timerSync();
forLoadingProgress();
MyApplication.showLoadingProgress();
String str1 = getSharedPreferences("DateTime", Context.MODE_PRIVATE).getString("user_time", "");
String str2 = "2013-07-14";
SQLiteDatabase sql2 = new SaveDataBase.SaveDateBaseHelp(this).getWritableDatabase();
Cursor select = sql2.rawQuery("SELECT * from db_pedometer_info ORDER BY pedometer_year, pedometer_month, pedometer_day DESC LIMIT 1; ", null);
if(select != null && select.getCount() == 1){
select.moveToFirst();
String year = select.getString(select.getColumnIndex("pedometer_year"));
String month = select.getString(select.getColumnIndex("pedometer_month"));
String day = select.getString(select.getColumnIndex("pedometer_day"));
str2 = year + "-" + month + "-" +day ;
}
我想在它出现时更改图标:
如果(MyApplication.isBluetoothConnection)
更新
这样应用就会崩溃:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.update_data:
MenuItem img = (MenuItem)findViewById(R.id.update_data);
((View) img).setBackgroundResource(R.drawable.sync);
AnimationDrawable frameAnimation = (AnimationDrawable) ((View) img).getBackground();
frameAnimation.start();
【问题讨论】:
-
Android 不支持 gif,但如果你有动画的帧,你可以使用 AnimationDrawable。
-
在其他活动上我有一个 gif,我将其设置为: webviewLoading.loadDataWithBaseURL("file:///android_asset/","
","text/html","utf-8",""); -
我怎样才能用图像而不是 gif 来做同样的事情?
-
我认为这个链接会有所帮助。您需要做的就是创建一个包含动画所有帧的动画列表,然后执行它developer.android.com/reference/android/graphics/drawable/…
-
我已经这样做了,但是我的应用程序崩溃了