Android自定义View 实现闹钟唤起播放闹钟铃声功能

先上图看一下闹钟唤期页面的效果

实现的功能:

1:转动的图片根据天气情况更换

2:转动时间可以设置,转动结束,闹铃声音就结束

3:光圈颜色渐变效果

直接上代码啦:

package com.yuekong.sirius.extension.customview;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;
import com.yuekong.sirius.extension.R;
import com.yuekong.sirius.extension.util.ExtendUtil;
import java.text.SimpleDateFormat;
/**
* Created by Zhongqi.Shao on 2016/12/5.
*/
public class ClockWakeView extends View {
//最外一层圆的颜色
private int mOutCircleColor;
//最外一层圆的半径
private int mOutCircleRadis;
//内圆的颜色
private int mInnerCircleColor;
//内圆的半径
private int mInnerCircleRadis;
private int mWidth;
private int mHeight;
//默认宽度
private final int DEFAULT_WIDTH = dp2px(240);
//默认高度
private final int DEFAULT_HEIGHT = dp2px(240);
//最外圆的默认半径
private int DEFAULT_OUT_RADIS = dp2px(120);
//内圆的默认半径
private int DeFAULT_INNER_RADIS = dp2px(105);
//标题距离顶部的默认距离
private int DEFAULT_TITLE_PADDING_TOP = dp2px(40);
//最外层圆形的Paint
private Paint mOutCirclePaint;
//内圆的Paint
private Paint mInnerCirclePaint;
//标题Paint
private Paint mTitlePaint;
//渐变弧
private Paint mGradientArcPaint;
private SweepGradient mSweepGradient;
//日期时间Paint
private Paint mTimePaint;
private Paint mDatePaint;
//时间分隔的图片
private Bitmap mDividerPic;
//天气图片
private Bitmap mWeatherPic;
//标题
private String mTitle;
//当前时间
private long mCurrentDate = 1481010829605L;
private SimpleDateFormat mFormat;
private Ringtone mRingTone;
//图片在倒计时几分钟内走完一个周期 根据音乐时间来计算
private long mCountDownTime;
//倒计时转过的角度
private float mCurrentAngle;
//倒计时结束回调
private CountdownFinishListener mCountdownListener;
private Context mContext;
public ClockWakeView(Context context) {
this(context, null);
mContext = context;
}
public ClockWakeView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
mContext = context;
}
public ClockWakeView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SiriusClockWakeView);
mOutCircleColor = ta.getColor(R.styleable.SiriusClockWakeView_outColor, context.getResources().getColor(R.color.out_circle_color));
mOutCircleRadis = (int) ta.getDimension(R.styleable.SiriusClockWakeView_outCircleRadis, DEFAULT_OUT_RADIS);
mInnerCircleColor = context.getResources().getColor(R.color.inner_circle_color);
mInnerCircleRadis = DeFAULT_INNER_RADIS;
mTitle = context.getResources().getString(R.string.clock);
ta.recycle();
initPaint();
}
private void initPaint() {
mFormat = new SimpleDateFormat("MM月dd日_HH_mm_EEEE");
//获取分隔图片
mDividerPic = BitmapFactory.decodeResource(getResources(), R.drawable.time_divider);
mWeatherPic = BitmapFactory.decodeResource(getResources(), R.drawable.icon_weather);
//外圆的paint
mOutCirclePaint = new Paint();
mOutCirclePaint.setAntiAlias(true);
mOutCirclePaint.setStyle(Paint.Style.STROKE);
mOutCirclePaint.setStrokeWidth(dp2px(5));
mOutCirclePaint.setColor(mOutCircleColor);
//内圆的Paint
mInnerCirclePaint = new Paint();
mInnerCirclePaint.setAntiAlias(true);
mInnerCirclePaint.setStyle(Paint.Style.STROKE);
mInnerCirclePaint.setStrokeWidth(dp2px(5));
mInnerCirclePaint.setColor(mInnerCircleColor);
//标题的Paint
mTitlePaint = new Paint();
mTitlePaint.setAntiAlias(true);
mTitlePaint.setStyle(Paint.Style.FILL);
mTitlePaint.setColor(mContext.getResources().getColor(R.color.nav_highlighted));
mTitlePaint.setTextSize(sp2px(14));
//时间的Paint
mTimePaint = new Paint();
mTimePaint.setAntiAlias(true);
mTimePaint.setColor(Color.WHITE);
mTimePaint.setStyle(Paint.Style.FILL);
mTimePaint.setTextSize(sp2px(70));
//日期的Paint
mDatePaint = new Paint();
mDatePaint.setAntiAlias(true);
mDatePaint.setColor(Color.WHITE);
mDatePaint.setStyle(Paint.Style.FILL);
mDatePaint.setTextSize(sp2px(14));
//渐变弧的Paint
mGradientArcPaint = new Paint();
mGradientArcPaint.setAntiAlias(true);
mGradientArcPaint.setStyle(Paint.Style.STROKE);
mGradientArcPaint.setStrokeWidth(dp2px(5));
mGradientArcPaint.setStrokeCap(Paint.Cap.BUTT);
mGradientArcPaint.setStrokeJoin(Paint.Join.MITER);
//设置渐变的颜色
int[] colors = {0x00AEA1FF, 0x40AEA1FF, 0xFFAEA1FF, 0xFFAEA1FF};
//渐变色
mSweepGradient = new SweepGradient(0, 0, colors, null);
mGradientArcPaint.setShader(mSweepGradient);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = startMeasure(widthMeasureSpec);
mHeight = startMeasure(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//移动画布到中间
canvas.translate(mWidth / 2, mHeight / 2);
//绘制最外层的圆形
drawOutCircle(canvas);
//绘制内圆
drawInnerCircle(canvas);
//绘制中间的分隔图片
drawDividerPic(canvas);
//绘制标题
drawTitle(canvas);
//绘制时间
drawDateTime(canvas);
//绘制旋转图片
drawLightArc(canvas);
}
private void drawOutCircle(Canvas canvas) {
canvas.save();
canvas.drawCircle(0, 0, mOutCircleRadis, mOutCirclePaint);
canvas.restore();
}
private void drawInnerCircle(Canvas canvas) {
canvas.save();
canvas.drawCircle(0, 0, mInnerCircleRadis, mInnerCirclePaint);
canvas.restore();
}
private void drawDividerPic(Canvas canvas) {
canvas.save();
canvas.drawBitmap(mDividerPic, null, new Rect(-dp2px(3), -dp2px(14), dp2px(3), dp2px(14)), null);
canvas.restore();
}
private void drawTitle(Canvas canvas) {
canvas.save();
if (mTitle == null || mTitle.length() <= 0) {
return;
}
float textWidth = mTitlePaint.measureText(mTitle);
float baseLine = DeFAULT_INNER_RADIS - DEFAULT_TITLE_PADDING_TOP;
canvas.drawText(mTitle, -textWidth / 2, -baseLine, mTitlePaint);
canvas.restore();
}
private void drawLightArc(Canvas canvas) {
canvas.save();
canvas.rotate(mCurrentAngle);
RectF rect = new RectF(-mInnerCircleRadis, -mInnerCircleRadis, mInnerCircleRadis, mInnerCircleRadis);
canvas.drawArc(rect, -270, 180, false, mGradientArcPaint);
canvas.drawBitmap(mWeatherPic, null, new Rect(-dp2px(12), -mInnerCircleRadis - dp2px(12), dp2px(12), dp2px(12) - mInnerCircleRadis), null);
canvas.restore();
}
private void drawDateTime(Canvas canvas) {
canvas.save();
String timeStr = ExtendUtil.getAllFormatStr(mFormat, mCurrentDate);
String[] array = timeStr.split("_");
String date = array[0];
String hour = array[1];
String minute = array[2];
String wake = array[3];
float width = mTimePaint.measureText(hour);
RectF targetRect = new RectF(-(width + dp2px(40)), -sp2px(70) / 2, -dp2px(40), sp2px(70) / 2);
Paint.FontMetricsInt fontMetrics = mTimePaint.getFontMetricsInt();
int baseline = (int) (targetRect.top + (targetRect.bottom - targetRect.top - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top);
canvas.drawText(hour, -width - dp2px(15), baseline, mTimePaint);
float minuteWidth = mTimePaint.measureText(minute);
RectF minuteRect = new RectF(dp2px(15), -sp2px(70) / 2, dp2px(15) + minuteWidth, sp2px(70) / 2);
Paint.FontMetricsInt minuteMetrics = mTimePaint.getFontMetricsInt();
int minuteLine = (int) (minuteRect.top + (minuteRect.bottom - minuteRect.top - minuteMetrics.bottom + minuteMetrics.top) / 2 - minuteMetrics.top);
canvas.drawText(minute, dp2px(15), minuteLine, mTimePaint);
String dateStr = date + " " + wake;
float dateWidth = mDatePaint.measureText(dateStr);
RectF dateRect = new RectF(-dateWidth / 2, sp2px(30), dateWidth / 2, sp2px(30) + sp2px(14));
Paint.FontMetricsInt dateMetrics = mTimePaint.getFontMetricsInt();
int dateLine = (int) (dateRect.top + (dateRect.bottom - dateRect.top - dateMetrics.bottom + dateMetrics.top) / 2 - dateMetrics.top);
canvas.drawText(dateStr, -dateWidth / 2, dateLine, mDatePaint);
canvas.restore();
}
private int startMeasure(int whSpec) {
int result = 0;
int size = MeasureSpec.getSize(whSpec);
int mode = MeasureSpec.getMode(whSpec);
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
result = DEFAULT_WIDTH;
}
return result;
}
//开始倒计时计算闹钟结束
public void startCountDown() {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1.0f);
animator.setDuration(mCountDownTime);
//匀速
animator.setInterpolator(new LinearInterpolator());
//不循环
animator.setRepeatCount(0);
//监听动画过程中值得实时变化
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mCurrentAngle = (float) valueAnimator.getAnimatedValue() * 360;
invalidate();
}
});
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
if (mRingTone != null && mRingTone.isPlaying()) {
mRingTone.stop();
}
if (mCountdownListener != null) {
mCountdownListener.countdownFinished();
}
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
animator.start();
//播放闹钟铃声
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
mRingTone = RingtoneManager.getRingtone(mContext, notification);
if (mRingTone != null && !mRingTone.isPlaying()) {
mRingTone.play();
}
}
//提供设置倒计时的方法
public void setCountDownTime(long time, CountdownFinishListener listener) {
mCountDownTime = time;
mCountdownListener = listener;
}
//设置当前日期时间
public void setCurrentTime(long currentTime) {
mCurrentDate = currentTime;
invalidate();
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
private int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
}
public interface CountdownFinishListener {
//倒计时结束操作
void countdownFinished();
}
}

以上所述是小编给大家介绍的Android自定义View 实现闹钟唤起播放闹钟铃声功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • Android编程使用AlarmManager设置闹钟的方法

    本文实例讲述了Android编程使用AlarmManager设置闹钟的方法.分享给大家供大家参考,具体如下: package com.Aina.Android; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * com.Aina.Android * Pro_AlarmM

  • Android编程闹钟设置方法详解

    本文实例讲述了Android编程闹钟设置方法.分享给大家供大家参考,具体如下: 闹钟在生活中最常见了,在Android中可以通过AlarmManager来实现闹钟,AlarmManager类专门用来设置在某个指定的时间去完成指定的时间.AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行.可以通过Context.getSystemService方法来获得该服务.AlarmManager中的方法不少,如下: 方法 说明 Cancel 取消

  • Android自定义闹钟功能

    Android闹钟alarmManager+PendingIntent的使用方法,供大家参考,具体内容如下 这次给大家分享两种闹钟: 第一种是当到达闹钟时间后执行一次相应的操作 alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent); 第二种是当到达闹钟时间后每隔一段时间执行操作 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeIn

  • Android闹钟启动时间设置无效问题的解决方法

    Android开发中,alarmManager在5.0以上系统,启动时间设置无效的问题 做一个app,需要后台保持发送心跳包.由于锁屏后CPU休眠,导致心跳包线程被挂起,所以尝试使用alarmManager定时唤醒Service发送心跳包. 以下是开启alarmManager的代码 //开启轮询服务 public static void startPollingService(Context context, int seconds, Class<?> cls,String action) {

  • Android设置闹钟相对完善的解决方案

    前些时候,有人在我「非著名程序员」微信公众号的后台问我有没有设置闹钟的demo,我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的方法即可.可以设置每天的闹钟,周一到星期天之间多选的闹钟,也可以设置选择一次性闹钟,跟系统设置闹钟的方式基本差不多吧. 效果图 来看分析和讲解之前,先看看效果吧,效果图如下: 设置界面 闹钟提醒界面 AlarmManag

  • android 设置闹钟及通知示例

    简单说一下这次demo内容,首先做一个设置一次性闹钟,先得到alarmManager,打开一个时间对话框,在里面设置闹钟的时间,时间一到发送广播,然后广播接受者接到跳转到新的activity播放音乐.接着是一个反复闹钟,最后是一个简单的通知,具体代码如下: import android.app.AlarmManager; import android.app.Notification; import android.app.NotificationManager; import android.

  • Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏

    实现闹钟运行的效果如下: 通知栏的运行后效果图如下: 布局文件(activity_main.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools&qu

  • Android编程实现闹钟的方法详解

    本文实例讲述了Android编程实现闹钟的方法.分享给大家供大家参考,具体如下: 在Android中,有一个闹钟的类,AlarmManager类,我们可以通过其来对系统内的通知进行操作! 本文用到了Service和Broadcast两个后台管理,在处理时,曾出现过很多问题,但经过半天的努力,终于解决了! 首先是Main.xml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns

  • Android闹钟设置的解决方案

    Android设置闹钟并不像IOS那样这么简单,做过Android设置闹钟的开发者都知道里面的坑有多深.下面记录一下,我解决Android闹钟设置的解决方案. 主要问题 1.API19开始AlarmManager的机制修改. 2.应用程序被Kill掉后,设置的闹钟不响. 3.6.0以上进入Doze模式会使JobScheduler停止工作. 4.手机设置重启后,闹钟失效问题. API19以上AlarmManager机制的修改 API19之前AlarmManager提供了三个设置闹钟的方法,由于业务

  • Android实现闹钟功能小Dome

    闹钟闹不醒的可以自己去调整下,这个最是最基本的 MainActivity public class MainActivity extends AppCompatActivity { private AlarmManager alarmManager; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst

随机推荐