Image become transparent while rotating on canvas

0

I'm making an analog clock Live wallpaper. AnalogClock extends Relative layout and later i'm drawing this view on canvas using wallpaper service. All code is working fine but there is some problem when its seconds needle reach at 3 (i.e 180 degree or 15 mins) its transparency becomes zero below is code sample for analog clock and wallpaper service .

AnalogClock.java

    public class AnalogClock extends RelativeLayout {
  private final AppCompatImageView face;
  private final AppCompatImageView hour;
  private final AppCompatImageView minute;
  private final AppCompatImageView second;

  private static final long HOURS = 3600000L;
  private static final long MINUTES = 60000L;
  private static final long SECONDS = 1000L;





  public AnalogClock(Context context) {
    this(context, null);
  }

  public AnalogClock(Context context,int position) {
    this(context, null);
  }

  public AnalogClock(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public AnalogClock(Context context, AttributeSet attrs, int defStyleAttr) {
    this(context, attrs, defStyleAttr, 0);
  }



  public AnalogClock(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    inflate(context, R.layout.analog_clock, this);


    face = findViewById(R.id.face);
    hour = findViewById(R.id.hour_hand);
    minute = findViewById(R.id.minute_hand);
    second = findViewById(R.id.second_hand);

    TypedArray typedArray = context.getTheme().obtainStyledAttributes(
        attrs, R.styleable.SimpleAnalogClock, defStyleAttr, defStyleRes);

    Drawable faceDrawable = typedArray.getDrawable(R.styleable.SimpleAnalogClock_faceDrawable);
    Drawable hourDrawable = typedArray.getDrawable(R.styleable.SimpleAnalogClock_hourDrawable);
    Drawable minuteDrawable = typedArray.getDrawable(R.styleable.SimpleAnalogClock_minuteDrawable);
    Drawable secondDrawable = typedArray.getDrawable(R.styleable.SimpleAnalogClock_secondDrawable);

    setFaceDrawable(faceDrawable != null ? faceDrawable : context.getDrawable(R.drawable.clock_00_face))
        .setHourDrawable(hourDrawable != null ? hourDrawable : context.getDrawable(R.drawable.clock_00_short))
        .setMinuteDrawable(minuteDrawable != null ? minuteDrawable : context.getDrawable(R.drawable.clock_00_long))
        .setSecondDrawable(secondDrawable != null ? secondDrawable : context.getDrawable(R.drawable.clock_00_second));

    int faceColor = typedArray.getColor(R.styleable.SimpleAnalogClock_faceTint, -1);
    int hourColor = typedArray.getColor(R.styleable.SimpleAnalogClock_hourTint, -1);
    int minuteColor = typedArray.getColor(R.styleable.SimpleAnalogClock_minuteTint, -1);
    int secondColor = typedArray.getColor(R.styleable.SimpleAnalogClock_secondTint, -1);
    if (faceColor != -1) setFaceTint(faceColor);
    if (hourColor != -1) setHourTint(hourColor);
    if (minuteColor != -1) setMinuteTint(minuteColor);
    if (secondColor != -1) setSecondTint(secondColor);

    rotateHourHand(typedArray.getFloat(R.styleable.SimpleAnalogClock_hourRotation, 0));
    rotateMinuteHand(typedArray.getFloat(R.styleable.SimpleAnalogClock_minuteRotation, 0));
    rotateSecondHand(typedArray.getFloat(R.styleable.SimpleAnalogClock_secondRotation, 0));
  }


  public AnalogClock setFaceDrawable(Drawable drawable) {
    face.setImageDrawable(drawable);
    return this;
  }


  public AnalogClock setHourDrawable(Drawable drawable) {
    hour.setImageDrawable(drawable);
    return this;
  }


  public AnalogClock setMinuteDrawable(Drawable drawable) {
    minute.setImageDrawable(drawable);
    return this;
  }


  public AnalogClock setSecondDrawable(Drawable drawable) {
    second.setImageDrawable(drawable);
    return this;
  }


  public AnalogClock rotateHourHand(float angle) {
    hour.setRotation(angle);
    return this;
  }


  public AnalogClock rotateMinuteHand(float angle) {
    minute.setRotation(angle);
    return this;
  }


  public AnalogClock rotateSecondHand(float angle) {
    second.setRotation(angle);
    Log.d("time","angle at  15->            "+angle);
    return this;
  }


  public AnalogClock setTime(int hour, int min, int seconds, int millis) {
    long hourMillis = hour * HOURS;
    long minMillis = min * MINUTES;
    long secondMillis = seconds * SECONDS;

    Log.d("time","time in  analog->     "+hour+":"+min+":"+seconds);

    rotateHourHand((float) 0.0000083 * (hourMillis + minMillis + secondMillis + millis));
    rotateMinuteHand((float) 0.0001 * (minMillis + secondMillis + millis));
    rotateSecondHand((float) 0.006 * (secondMillis + millis));
    return this;
  }


  public AnalogClock setTime(int hour, int min, int seconds) {
    return setTime(hour, min, seconds, 0);
  }


  public AnalogClock setFaceTint(int color) {
    face.setColorFilter(color);
    return this;
  }


  public AnalogClock setHourTint(int color) {
    hour.setColorFilter(color);
    return this;
  }


  public AnalogClock setMinuteTint(int color) {
    minute.setColorFilter(color);
    return this;
  }


  public AnalogClock setSecondTint(int color) {
    second.setColorFilter(color);
    return this;
  }
}

ClockWallpaperService.java

public class ClockWallpaperService extends WallpaperService {



    @Override
    public Engine onCreateEngine() {

        return new ClockWallpaperEngine();
    }

    private class ClockWallpaperEngine extends Engine {
        private final Handler handler = new Handler();
        int passedSeconds;
        private final Runnable drawRunner = new Runnable() {
            @Override
            public void run() {
                passedSeconds++;
                draw();
            }

        };

        int [] clockDialList= new int[]{
                R.drawable.clock11,
                R.drawable.clock22,
                R.drawable.clock33,
                R.drawable.clock44,
                R.drawable.clock55,
                R.drawable.clock66
        };



        int [] digitalClockColor=new int[]{
                getResources().getColor(R.color.color_clock1),
                getResources().getColor(R.color.color_clock2),
                getResources().getColor(R.color.color_clock3),
                getResources().getColor(R.color.color_clock4),
                getResources().getColor(R.color.color_clock5),
                getResources().getColor(R.color.color_clock6)

        };


        private Paint paint,rectPaint;
        /** hands colors for hour, min, sec */
        private int[] colors = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
        private int bgColor;
        private int width;
        private int height;
        private boolean visible = true;

        private AnalogClock clockViewGroup;

        Rect rect,textRect ;

        TextView timeView,dateView;
        Calendar calendar;



        public ClockWallpaperEngine() {

            clockViewGroup=null;
            timeView =null;
            dateView=null;


            final AppPreferences appPreferences = new AppPreferences(getApplicationContext()); 
            final int value = appPreferences.getInt("pos", 0);
            type=appPreferences.getString("type", ConstantsKt.ANALOG);

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(5);


            bgColor = Color.parseColor("#000000");
            passedSeconds=getPassedSeconds();

            Log.d("GOT_DATA", "I GOT THIS POSITION WITH PREFERENCE:  " + value);
            Log.d("GOT_DATA","Received type = "+type);

            if(type.equals(ConstantsKt.ANALOG)){

                rect= new Rect();
                rect.set(0, 0, 300, 300);
                if(value<clockDialList.length){

                    clockViewGroup=new AnalogClock(getApplicationContext());
                    clockViewGroup.setFaceDrawable(getResources().getDrawable(clockDialList[value],null));

                }else {
                    clockViewGroup.setFaceDrawable(getResources().getDrawable(clockDialList[0],null));

                }
            }else {

                calendar = Calendar.getInstance();


                textRect=new Rect();

                timeView = new TextView(getApplicationContext());
                timeView.setTextColor(digitalClockColor[value]);
                timeView.setTextSize(getResources().getDimension(R.dimen._40ssp));
                Typeface plain;

                if(Build.VERSION.SDK_INT>=26){
                    plain = getResources().getFont(R.font.ds_digital);
                }else {
                    plain = Typeface.createFromAsset(getResources().getAssets(), "font/ds_digital.TTF");
                }

                timeView.setTypeface(plain);
                timeView.setGravity(Gravity.CENTER);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                timeView.setLayoutParams(params);




                dateView = new TextView(getApplicationContext());
                dateView.setTextColor(digitalClockColor[value]);
                dateView.setTextSize(getResources().getDimension(R.dimen._15ssp));
                dateView.setTypeface(plain);
                dateView.setGravity(Gravity.CENTER_HORIZONTAL);
                dateView.setLayoutParams(params);
            }


            handler.post(drawRunner);

        }


        @Override
        public void onVisibilityChanged(boolean visible) {
            this.visible = visible;
            if (visible) {
                handler.post(drawRunner);
            } else {
                handler.removeCallbacks(drawRunner);
            }
        }

        @Override
        public void onSurfaceDestroyed(SurfaceHolder holder) {
            super.onSurfaceDestroyed(holder);
            this.visible = false;
            handler.removeCallbacks(drawRunner);

        }

        @Override
        public void onSurfaceChanged(SurfaceHolder holder, int format,
                                     int width, int height) {
            this.width = width;
            this.height = height;


            if(clockViewGroup!=null){
                //Measure the view at the exact dimensions (otherwise the text won't center correctly)
                int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), View.MeasureSpec.EXACTLY);
                int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), View.MeasureSpec.EXACTLY);

                clockViewGroup.measure(widthSpec, heightSpec);
                //Lay the view out at the rect width and height
                clockViewGroup.layout(0, 0, rect.width(), rect.height());

            }else {


                Log.d("width_height","width:  "+width+"\nheight:  "+height);
                textRect.set(0,0,width,height/2);

                //Measure the view at the exact dimensions (otherwise the text won't center correctly)
                int widthSpec = View.MeasureSpec.makeMeasureSpec(textRect.width(), View.MeasureSpec.EXACTLY);
                int heightSpec = View.MeasureSpec.makeMeasureSpec(textRect.height(), View.MeasureSpec.EXACTLY);


                Log.d("width_height","width_spec:  "+width+"\nheight_spec:  "+height);
                timeView.measure(widthSpec,heightSpec);
                timeView.layout(0,0,textRect.width(),textRect.height());

            }

            super.onSurfaceChanged(holder, format, width, height);
        }

        private void draw() {
            SurfaceHolder holder = getSurfaceHolder();
            Canvas canvas = null;
            try {
                canvas = holder.lockCanvas();
                if (canvas != null) {
                    draw(canvas);
                }
            } finally {
                if (canvas != null)
                    holder.unlockCanvasAndPost(canvas);
            }

            handler.removeCallbacks(drawRunner);

            if (visible) {
                handler.postDelayed(drawRunner, 1000L);
            }
        }

        private void draw(Canvas canvas) {
            canvas.drawColor(bgColor);

            int hours = (passedSeconds / 3600) % 24;
            int minutes = (passedSeconds / 60) % 60;
            int seconds = passedSeconds % 60;

            Log.d("time","time in  service->            "+hours+":"+minutes+":"+seconds);

            if(clockViewGroup!=null){

                int centerX=canvas.getWidth()/2;
                int centerY=canvas.getHeight()/3;

                clockViewGroup.setTime(hours,minutes,seconds);


                //Translate the Canvas into position and draw it
                canvas.save();
                canvas.translate(centerX-(rect.width()/2), centerY-(rect.height()/2));
                clockViewGroup.draw(canvas);
                canvas.restore();


            }else {

                timeView.setText(getFormattedTimeDate(getApplicationContext(),passedSeconds,true,true));
                timeView.draw(canvas);

            }

        }

    }

}
android
android-studio
matrix
canvas
viewgroup

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0