在类似小部件的Tripadvisor应用程序上刷新视图

时间:2015-03-06 08:01:44

标签: android android-widget

我有一个小部件,显示我网站的最后一项。有一个图像视图可以在点击它后更改窗口小部件的值(图像,标题等)。

现在,我想创建一个像Tripadvisor应用程序一样的刷新按钮(旋转图标,直到获得一些内容)。

我可以使用启动和停止功能创建一个imageview来处理这个旋转图标吗?

更新

我认为,在小部件中,我无法直接访问视图。

    remoteViews = new RemoteViews(context.getPackageName(),
            R.layout.widgets_layout);

enter image description here

2 个答案:

答案 0 :(得分:0)

你应该像这样使用旋转动画

RotateAnimation rotateAnim = new RotateAnimation(0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

    rotateAnim .setDuration(4000);
    rotateAnim .setRepeatCount(-1);
    yourView.setAnimation(rotateAnim );

并像这样停止

yourView.clearAnimation();

希望它有所帮助!

答案 1 :(得分:0)

您可以使用在后台线程上运行的AsyncTask和ProgressBar。

这不是确切的代码,但它是我使用过的一个例子。

private void updatePicture(String url){

   AsyncTask<String, Void, Bitmap> getNewPicture = new AsyncTask<String, Void, Bitmap>() {

    /**
     * Your "start" method
     */
    @Override
    protected void onPreExecute() {

        // show the rotating icon
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setActivated(true);
    }

    /**
     * The main task
     */
    protected Bitmap doInBackground(String... urls) {
        //get the updated icon...
        String urldisplay = urls[0];
        .....

        return updatedPicture;
    }

    /**
     * The "stop" method
     */
     protected void onPostExecute(Bitmap result) {
         // hide ProgressBar and show image
         progressBar.setVisibility(View.GONE);
         bmImage.setImageBitmap(result);
     }      

    //run the task
    getNewPicture.execute(urlForUpdate);
}

您需要在自己的方法中使用AsyncTask,否则如果您尝试多次运行它,您将获得异常。这样,每次调用UpdatePicture()时,都会创建并运行一个新的AsyncTask。

相关问题