Advertisement
Guest User

TimeSettableAnalogClockService

a guest
Aug 15th, 2012
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package com.example.anlogclocksettimeexample;
  2.  
  3. import java.util.Date;
  4.  
  5. import android.app.Service;
  6. import android.appwidget.AppWidgetManager;
  7. import android.content.ComponentName;
  8. import android.content.Intent;
  9. import android.content.res.Resources;
  10. import android.graphics.Bitmap;
  11. import android.graphics.Canvas;
  12. import android.graphics.drawable.BitmapDrawable;
  13. import android.graphics.drawable.Drawable;
  14. import android.os.IBinder;
  15. import android.text.format.DateFormat;
  16. import android.text.format.Time;
  17. import android.util.TypedValue;
  18. import android.widget.AnalogClock;
  19. import android.widget.RemoteViews;
  20. import android.widget.Toast;
  21.  
  22. public class TimeSettableAnalogClockService extends Service {
  23.  
  24.     private Time mCalendar;
  25.     private float mMinutes;
  26.     private float mHour;   
  27.    
  28.     @Override
  29.     public int onStartCommand(Intent intent, int flags, int startId) {
  30.        
  31.         buildUpdate();
  32.        
  33.         return super.onStartCommand(intent, flags, startId);
  34.     }
  35.    
  36.     private void buildUpdate() {
  37.         RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.appwidget);
  38.        
  39.         mCalendar = new Time();
  40.        
  41.         // Line below sets time that will be displayed - modify as needed.
  42.         mCalendar.setToNow();
  43.  
  44.         int hour = mCalendar.hour;
  45.         int minute = mCalendar.minute;
  46.         int second = mCalendar.second;
  47.  
  48.         mMinutes = minute + second / 60.0f;
  49.         mHour = hour + mMinutes / 60.0f;
  50.        
  51.         Resources r = getApplicationContext().getResources();
  52.        
  53.        
  54.         // You'll have to determine tour own dimensions of space to which analog clock is drawn.
  55.         final int APP_WIDGET_WIDTH_DP = 210; // Taken from widget_provider.xml: android:minWidth="210dp"
  56.         final int APP_WIDGET_HEIGHT_DP = 210; // Taken from widget_provider.xml: android:minHeight="210dp"
  57.  
  58.         final int availableWidth = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, APP_WIDGET_WIDTH_DP, r.getDisplayMetrics()) + 0.5f);
  59.         final int availableHeight = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, APP_WIDGET_HEIGHT_DP, r.getDisplayMetrics()) + 0.5f);
  60.  
  61.         int x = availableWidth / 2;
  62.         int y = availableHeight / 2;
  63.  
  64.        
  65.         // Creating Bitmap and Canvas to which analog clock will be drawn.
  66.         Bitmap appWidgetBitmap = Bitmap.createBitmap(availableWidth, availableHeight, Bitmap.Config.ARGB_8888);
  67.         Canvas canvas = new Canvas(appWidgetBitmap);
  68.        
  69.         final Drawable dial = r.getDrawable(R.drawable.clock_dial);
  70.         int w = dial.getIntrinsicWidth();
  71.         int h = dial.getIntrinsicHeight();
  72.                        
  73.         boolean scaled = false;
  74.  
  75.         if (availableWidth < w || availableHeight < h) {
  76.             scaled = true;
  77.             float scale = Math.min((float) availableWidth / (float) w,
  78.                                    (float) availableHeight / (float) h);
  79.             canvas.save();
  80.             canvas.scale(scale, scale, x, y);
  81.         }
  82.  
  83.         dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
  84.         dial.draw(canvas);
  85.  
  86.         canvas.save();
  87.         canvas.rotate(mHour / 12.0f * 360.0f, x, y);
  88.        
  89.         final Drawable hourHand = r.getDrawable(R.drawable.clock_hand_hour);
  90.         w = hourHand.getIntrinsicWidth();
  91.         h = hourHand.getIntrinsicHeight();
  92.         hourHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
  93.        
  94.         hourHand.draw(canvas);
  95.         canvas.restore();
  96.  
  97.         canvas.save();
  98.         canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);
  99.  
  100.         final Drawable minuteHand = r.getDrawable(R.drawable.clock_hand_minute);
  101.         w = minuteHand.getIntrinsicWidth();
  102.         h = minuteHand.getIntrinsicHeight();
  103.         minuteHand.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
  104.         minuteHand.draw(canvas);
  105.         canvas.restore();
  106.  
  107.         if (scaled) {
  108.             canvas.restore();
  109.         }      
  110.        
  111.         remoteViews.setImageViewBitmap(R.id.imageView1, appWidgetBitmap);
  112.        
  113.         // Pushing widget update to home screen
  114.         ComponentName widgetComponentName = new ComponentName(this, TimeSettableAnalogClockAppWidget.class);
  115.         AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
  116.         appWidgetManager.updateAppWidget(widgetComponentName, remoteViews);    
  117.        
  118.     }
  119.  
  120.     @Override
  121.     public IBinder onBind(Intent intent) {
  122.         return null;
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement