【问题标题】:Get ImageView drawable ID and change it with AsyncTask获取 ImageView 可绘制 ID 并使用 AsyncTask 更改它
【发布时间】:2013-09-27 03:17:52
【问题描述】:

我想要做的:获取 ImageView 的 src 的 id,将其与两个可绘制对象的 id 进行比较,然后使用 AsyncTask 交换它们(只是因为我想了解它是如何工作的)。 我在这里读过类似的问题,到目前为止,这就是我所得到的:

public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView image = (ImageView) findViewById(R.id.img);
    Integer integer = (Integer) image.getTag();
}

private class cambiarImagen extends AsyncTask<Integer, Integer, Integer> {
    protected void onPreExecute() {
        ImageView image = (ImageView) findViewById(R.id.img);
        Integer integer = (Integer) image.getTag();
        int img1 = R.drawable.zapato;
        int img2 = R.drawable.zapatod;
    }

    @Override
    protected Integer doInBackground(Integer... values) {
        // parte logica
        int num = values[0];
        int zapato = values[1];
        int zapatod = values[2];
        if (num == zapato) {
            num = zapatod;
        } else if (num == zapatod) {
            num = zapato;
        }
        return num;
    }
    protected Void onPostExecute(Integer... values) {
        int num = values[0];
        ImageView image = (ImageView) findViewById(R.id.img);
        image.setTag(num);
        return null;
    }
}

这当然行不通。 1.我不明白如何获取ImageView作为其src的drawable的id。 2.不明白AsyncTask中params是怎么传递的; onPreExecute 应该接收 UI 的东西,doInbackground 应该接收它来比较它并返回应该设置为 ImageView 的 drawable int,onPreExecute 应该将它设置为 ImageView。

【问题讨论】:

  • 对于初学者,您可以省略参数并使用其他版本的 AsyncTask(非泛型)。然后,您可以在 Main 中声明私有成员并在启动 AsyncTask 之前将值存储在其中,并且由于它是内部类,因此可以访问这些成员。
  • 我不认为可以在运行时确定视图的背景 ID。但是,这应该是已知的,因为它通常设置在布局或 onCreate 中,并且如果您的编码“正确”,该值将在资源文件 (R.Drawable.bkground) 中定义。我通常不会遇到我不知道值是什么的情况,如果它发生变化(比如白色/启用到灰色/禁用),那么您可以通过初始化为一种颜色来跟踪,然后根据其他条件(通常由用户输入决定,而不是当前背景)。

标签: java android android-asynctask imageview


【解决方案1】:

我不明白如何获取 ImageView 作为其 src 的可绘制对象的 id。

我不必这样做,所以可能行不通,但你应该可以使用

imageView.getDrawable().getId();

我不明白参数是如何在 AsyncTask 中传递的;

无论你在task.execute() 中传递什么,doInBackground() 都会收到。如果你打电话给publishProgress(),那么onProgressUpdate() 会收到那里发送的任何参数。而doInBackground()中返回的数据由onPostExecute()接收。

AsyncTask,你知道,这不应该是必要的,但我知道你说过你想学习如何使用它。除了这两件事之外,我对您具体遇到的问题有点困惑,所以如果我遗漏了什么,请详细说明。

ImageView Docs

AsyncTask Docs

AsyncTask Example(以防万一)

【讨论】:

    【解决方案2】:

    如果你想学习 ASyncTask,你应该做其他任务。 如果我想学习 ASyncTask,我会做一个带有进度条或其他东西的对话框。

    编辑: 正如 Samus Arin 在主帖中评论的那样,您应该始终跟踪您正在展示的图像。所以改为使用类似

    if(currentImage == R.Drawable.image1){
    
    image.setImageResource(R.Drawable.image2);
    
    }else{
    
        image.setImageResource(R.Drawable.image1);
    
    }
    

    【讨论】:

    • 谢谢,但 getDrawable 的方法 getID 未定义。
    • Arr,我刚刚删除了关于 OP b/c 的评论,您进行了编辑,说明它确实有效!我认为没有(android 喜欢返回 null/0)。
    • 确实编辑了文本,我同意 Samus Arin 对主帖的评论。
    • @m.Ling 为了消除任何混淆,我上面的评论是针对 OP,而不是你 :) 他添加了一个编辑,显示他声称有效的代码,其中包括一行“int id = view .getDrawabled().getId()”,所以我删除了我的评论,指出它不能完成(但随后重新添加)。
    【解决方案3】:

    对于它的价值,看看我正在用 AsyncTask 做什么,也许它会给你一些想法。

    这是 Monodroid/C# 代码,不是原始的 Java/Android(但语法非常接近)。因此,内部类不会获得对其包含对象的隐式引用,因此我传入一个(在构造函数中称为外部)。我选择将其命名为“_”作为 .NET 的私有数据成员的 _member 命名约定的字典扩展。

    public class MonthChangeTask : AsyncTask
    {
        private CalendarView _;     // outer class
        private DateTime _newMonth;
        private bool _refreshInspectionRecordsRemote;
        private bool _changingInspector;
        private bool _todayButtonPressed;
    
        private Android.App.ProgressDialog _progressDialog;
    
        private IMXController _controller;
        private Dictionary<string, string> _paramz;
        private DateTime _newSelectedDate;
    
        public MonthChangeTask( CalendarView outer, DateTime newMonth, bool changingInspector, bool refreshInspectionRecordsRemote, bool todayButtonPressed )
        {
            _ = outer;
            _newMonth = newMonth;
            _changingInspector = changingInspector;
            _refreshInspectionRecordsRemote = refreshInspectionRecordsRemote;
            _todayButtonPressed = todayButtonPressed;
        }
    
        protected override void OnPreExecute()
        {
            base.OnPreExecute();
    
            _progressDialog = Android.App.ProgressDialog.Show( _ , "", "Loading Inspections...");
    
            _newSelectedDate = _._calendar.SetMonth(new DateTime(_newMonth.Year, _newMonth.Month, 1));
    
            AppSettingService.SetCalendarDate(_newMonth);
    
            _paramz = new Dictionary<string, string>();
    
            string target = MD.MxNAVIGATION.CONTROLLER.CALENDAR._name;
            string action = MD.MxNAVIGATION.CONTROLLER.ACTION.GET;
            string command = _refreshInspectionRecordsRemote
                ? ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsRemote).ToString()
                : ((int) MD.MxNAVIGATION.CONTROLLER.CALENDAR.Command.RefreshInspectionRecordsLocal).ToString();
    
            string url = target + "/" + action + "/" + command;
    
            _controller = MXContainer.Instance.GetController(url, ref _paramz);
        }
    
        protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params)
        {
            if ( _paramz == null )
            {
                Log.Info(FIDB.TAG_APP, "MonthChangeTask.DoInBackground(): paramz = NULL");
            }
            else
            {
                _controller.Load( _paramz );
            }
    
            return true;
        }
    
        protected override void OnPostExecute(Java.Lang.Object result)
        {
            base.OnPostExecute(result);
    
            _progressDialog.Dismiss();
    
            _.Model = (CalendarVM)_controller.GetModel();   
    
            if (_changingInspector)
            {
                _._calendar.PermitSwitch = _.Model.Buttons.PermitsVisible;
                _._calendar.ComplaintSwitch = _.Model.Buttons.ComplaintsVisible;
                _._calendar.ProjectSwitch = _.Model.Buttons.ProjectsVisible;
                _._calendar.PeriodicInspectionSwitch = _.Model.Buttons.PeriodicInspectionsVisible;
            }
    
            _.UpdateCalendar(_.Model.Inspections);
    
            if( _todayButtonPressed )
            {
                _._calendar.SelectedDate = _._calendar.CurrentDate;
            }
            else
            {
                _._calendar.SelectedDate = _newSelectedDate;            
            }
    
            _._calendar.Invalidate();
            AppSettingService.SetCalendarDate( _._calendar.SelectedDate );
    
            if ( _.Model.IsParcelCacheDownloading )
            {
                AnimationTask task = new AnimationTask( _ );
                task.Execute( new Java.Lang.Object[1] );
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多