• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Widget-Image Rotate

jagathyks

Lurker
I am a new comer. I wants to create a custom clock widget. Firstly I try to rotate one needle. There is no error found. But IMAGEVIEW stands still. I want some help.
thanks in advance.
Here is my widget.java.

Code:
 public class widget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    private class MyTime extends TimerTask {
    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    Bitmap bitmap;
    
    private Time mCalendar; 
   
    private float mSecond;
    private ImageView mMainImg;
    
    
    public MyTime(Context context, AppWidgetManager appWidgetManager) {
    this.appWidgetManager = appWidgetManager;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    thisWidget = new ComponentName(context, widget.class);
    bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.id.imgMinute);    
    }

    @Override
    public void run() {        
        mCalendar =new Time();
        onTimeChanged();
        rotate(bitmap);        
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);    
    }
    
    public  void rotate(Bitmap bitmap){        
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.preRotate(mSecond  * 360.0f);
                Bitmap rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
                true);
                BitmapDrawable bdr = new BitmapDrawable(rotaBitmap);
                mMainImg.setImageDrawable(bdr);
    }
    
     private void onTimeChanged() {
               mCalendar.setToNow();
            int second = mCalendar.second;            
            mSecond= second / 60.0f;         
        }
    
    }


}
 
try adding mMainImage.invalidate() at the end of your rotate method and see what happens.

Thanks. But got this error.
FATAL EXCEPTION: Timer-0
java.lang.NullPointerException
MyTime.rotate(widget.java:56)
AndroidRuntime(15515):at java.util.Timer$TimerImpl.run(Timer.java:284)
 
Oops, I meant mMainImg.invalidate(), though looking back through your code, you should have been getting a NullPointerException before adding that line, as you never initialize mMainImg. You need to somehow make mMainImg point to your image view.

Furthermore, you cannot modify UI components in any thread by the UI thread. Since a Timer object spawns another thread using a TimerTask as its Runnable, you need to use a Handler object to post back to the UI thread, notifying it that the ImageView needs updated.
 
Oops, I meant mMainImg.invalidate(), though looking back through your code, you should have been getting a NullPointerException before adding that line, as you never initialize mMainImg. You need to somehow make mMainImg point to your image view.

Furthermore, you cannot modify UI components in any thread by the UI thread. Since a Timer object spawns another thread using a TimerTask as its Runnable, you need to use a Handler object to post back to the UI thread, notifying it that the ImageView needs updated.

I don't know much about java programming. I am just a beginner.
This is my modified code.

Code:
public class widget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);    
    }
    private class MyTime extends TimerTask {
    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    Bitmap bitmap;
    
    private Time mCalendar;    

    private float mMinutes;
    private float mHour;
    private ImageView mMainImg;
    
    
    public MyTime(Context context, AppWidgetManager appWidgetManager) {
    this.appWidgetManager = appWidgetManager;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    thisWidget = new ComponentName(context, widget.class);
    bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.id.imgMinute);    
    }
    @Override
    public void run() {        
        mCalendar =new Time();
        onTimeChanged();
        rotate(bitmap);        
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);    
    }
    
    public  void rotate(Bitmap bitmap){        
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.preRotate(mMinutes / 60.0f * 360.0f);
                Bitmap rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
                true);
                BitmapDrawable bdr = new BitmapDrawable(rotaBitmap);
                mMainImg.setImageDrawable(bdr);
                mMainImg.invalidate();
    }
    
     private void onTimeChanged() {
               mCalendar.setToNow();

            int hour = mCalendar.hour;
            int minute = mCalendar.minute;
            int second = mCalendar.second;
            mMinutes = minute + second / 60.0f;            
            mHour = hour + mMinutes / 60.0f;
            }
    
    }


}

Error

FATAL EXCEPTION: Timer-0
java.lang.NullPointerException
MyTime.rotate(widget.java:56)
05-16 11:03:39.710: E/AndroidRuntime(15515): at java.util.Timer$TimerImpl.run(Timer.java:284)

code at line number 56 (widget.jave.56) is int w = bitmap.getWidth();
 
I am somewhat shooting in the dark here, since I have never really worked with AppWidgets before and on top of that, I don't know how your resources are set up, but I will try to help the best I can. Try this:

Code:
public class widget extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);    
    }
    private class MyTime extends TimerTask {
    RemoteViews remoteViews;
    AppWidgetManager appWidgetManager;
    ComponentName thisWidget;
    Bitmap bitmap;
    
    private Time mCalendar;    

    private float mMinutes;
    private float mHour;
    private ImageView mMainImg;
    
    
    public MyTime(Context context, AppWidgetManager appWidgetManager) {
    this.appWidgetManager = appWidgetManager;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    
    thisWidget = new ComponentName(context, widget.class);
    bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.imgMinute);    

    mMainImg = remoteViews.setImageViewBitmap(R.id.ID_OF_IMAGEVIEW, bitmap);
    }
    @Override
    public void run() {        
        mCalendar =new Time();
        onTimeChanged();
        rotate(bitmap);        
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);    
    }
    
    public  void rotate(Bitmap bitmap){        
                int w = bitmap.getWidth();
                int h = bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.preRotate(mMinutes / 60.0f * 360.0f);
                Bitmap rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
                true);
                BitmapDrawable bdr = new BitmapDrawable(rotaBitmap);
                mMainImg.setImageDrawable(bdr);
                mMainImg.invalidate();
    }
    
     private void onTimeChanged() {
               mCalendar.setToNow();

            int hour = mCalendar.hour;
            int minute = mCalendar.minute;
            int second = mCalendar.second;
            mMinutes = minute + second / 60.0f;            
            mHour = hour + mMinutes / 60.0f;
            }
    
    }

[/cod]
}

note that ID_OF_IMAGEVIEW is the id mMainImg as defined in your layout xml.
 
error at:
mMainImg = remoteViews.setImageViewBitmap(R.id.ID_OF_IMAGEVIEW, bitmap);

says cannot convert void to imageview
 
Back
Top Bottom