簡體   English   中英

獲取ImageView可繪制ID,並使用AsyncTask進行更改

[英]Get ImageView drawable ID and change it with AsyncTask

我想做的是:獲取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的可繪制對象的ID。 2.我不明白如何在AsyncTask中傳遞參數; onPreExecute應該接收UI內容,doInbackground應該接收它進行比較,並返回應設置為ImageView的drawable int,onPreExecute應該將其設置為ImageView。

我不明白如何獲取ImageView作為其src的可繪制對象的ID。

我不必這樣做,因此可能無法正常工作,但您應該可以使用

imageView.getDrawable().getId();

我不明白如何在AsyncTask中傳遞參數;

無論您傳入task.execute()是什么,都將被doInBackground()接收。 如果您調用publishProgress()則無論發送什么參數, onProgressUpdate()接收到。 doInBackground()返回的數據由onPostExecute()接收。

AsyncTask ,不需要AsyncTask ,但我知道您說過您想學習如何使用它。 除了這兩件事,我對您具體遇到的問題還是有些困惑,因此,如果我錯過了什么,請詳細說明。

ImageView文件

AsyncTask文件

AsyncTask示例 (如果有幫助的話)

如果您想學習ASyncTask,則應該執行其他任務。 如果我想學習ASyncTask,我會用進度條或其他方法完成對話框。

編輯:正如薩姆斯·阿林(Samus Arin)在關於您的主要文章上發表評論時,應始終跟蹤所顯示的圖像。 所以改用類似

if(currentImage == R.Drawable.image1){

image.setImageResource(R.Drawable.image2);

}else{

    image.setImageResource(R.Drawable.image1);

}

對於它的價值,看看我在用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] );
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM