drawf / sourceset Goto Github PK
View Code? Open in Web Editor NEW我的Android开源代码集合
我的Android开源代码集合
package com.ljy.piecharviewdemo;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.LogRecord;
public class MyLwp extends WallpaperService {
private float mHeight,mWidth;
private static final String TAG = "ClockView";
Paint helperPaint;
Paint mPaint;
static ValueAnimator valueAnimator;
private float mHourR,minuteR,secondR;
private float mHourDeg,minuteDeg,secondDeg;
private int hour,minute,second;
private float hd,md,sd;
Runnable drawTarget;
private static final String[] NUMBER_TEXT_LIST = {"日","一", "二", "三", "四", "五", "六", "七", "八", "九","十"};
@Override
public Engine onCreateEngine() {
return new MyEngine();
}
class MyEngine extends Engine{
private Timer mTimer = null;
private Handler handler = new Handler();
@Override
public SurfaceHolder getSurfaceHolder() {
return super.getSurfaceHolder();
}
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
mWidth = displayMetrics.widthPixels;
mHeight = displayMetrics.heightPixels;
mHourR = mWidth * 0.143f;
minuteR = mWidth *0.35f;
secondR = mWidth * 0.35f;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onVisibilityChanged(boolean visible) {
super.onVisibilityChanged(visible);
if (visible) {
startClock();
} else {
stopClock();
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
}
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
}
private void startClock() {
mTimer = new Timer(false);
mTimer.schedule(new TimerTask() {
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
hour = calendar.get(Calendar.HOUR);
minute = calendar.get(Calendar.MINUTE);
second = calendar.get(Calendar.SECOND);
mHourDeg = -360/12f*(hour - 1);
minuteDeg = -360/60f * (minute - 1);
secondDeg = -360/60f * (second - 1);
Canvas canvas = getSurfaceHolder().lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.save();
//将画布移到中心
canvas.translate(mWidth/2,mHeight/2);
initPaint();
mPaint.setTextSize(mHourR*0.16f);
drawLiner(canvas);
drawHour(canvas,mHourDeg);
drawMinute(canvas,minuteDeg);
drawSecond(canvas,secondDeg);
canvas.drawText("现在是"+second,0,100,mPaint);
getSurfaceHolder().unlockCanvasAndPost(canvas);
}
},0,10);
}
private void stopClock() {
mTimer.cancel();
mTimer = null;
}
/**
* 初始化画笔
*
*/
private void initPaint() {
//参考线
helperPaint = new Paint();
helperPaint.setColor(Color.RED);
helperPaint.setStyle(Paint.Style.STROKE);
//文字
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
//是否抗锯齿
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
}
/**
* 绘制小时
* @param canvas
* @param degrees
*/
private void drawHour(Canvas canvas,float degrees) {
mPaint.setTextSize(mHourR*0.16f);
//处理旋转
canvas.save();
Log.d(TAG, "drawHour: degrees="+degrees);
canvas.rotate(degrees);
for (int i=0;i<12;i++) {
canvas.save();
int iDeg = 360/12*i;
canvas.rotate(iDeg);
if (iDeg+degrees == 0f) {
mPaint.setAlpha(255);
}else{
mPaint.setAlpha(153);
}
mPaint.setTextAlign(Paint.Align.LEFT);
float y =mPaint.getFontSpacing()/2-mPaint.getFontMetrics().bottom;
canvas.drawText(getCnStr(i+1)+"点",mHourR,y,mPaint);
canvas.restore();
}
canvas.restore();
}
/**
* 绘制分
* @param canvas
* @param degrees
*/
private void drawMinute(Canvas canvas,Float degrees) {
mPaint.setTextSize(mHourR*0.16f);
//处理整体旋转
canvas.save();
canvas.rotate(degrees);
for (int i=0;i<60;i++) {
canvas.save();
float iDeg = 360 /60f*i;
canvas.rotate(iDeg);
if (iDeg+degrees == 0f) {
mPaint.setAlpha(255);
}else{
mPaint.setAlpha(153);
}
mPaint.setTextAlign(Paint.Align.RIGHT);
float y =mPaint.getFontSpacing()/2-mPaint.getFontMetrics().bottom;
if (i<59) {
canvas.drawText(getCnStr(i+1)+"分",minuteR,y,mPaint);
}
canvas.restore();
}
canvas.restore();
}
/**
* 绘制秒
* @param canvas
* @param degrees
*/
private void drawSecond(Canvas canvas,Float degrees) {
mPaint.setTextSize(mHourR * 0.16f);
//处理整体旋转
canvas.save();
canvas.rotate(degrees);
for (int i = 0;i<60;i++) {
canvas.save();
float iDeg = 360 /60f*i;
canvas.rotate(iDeg);
if (iDeg+degrees == 0f) {
mPaint.setAlpha(255);
}else{
mPaint.setAlpha(153);
}
mPaint.setTextAlign(Paint.Align.LEFT);
float y =mPaint.getFontSpacing()/2-mPaint.getFontMetrics().bottom;
if (i<59) {
canvas.drawText(getCnStr(i+1)+"秒",secondR,y,mPaint);
}
canvas.restore();
}
canvas.restore();
}
/**
* 绘制圆内信息
* @param canvas
*/
private void drawLiner(Canvas canvas) {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
mPaint.setTextSize(mHourR*0.4f);
mPaint.setAlpha(255);
//设置文本对齐
mPaint.setTextAlign(Paint.Align.CENTER);
float y =-mPaint.getFontMetrics().bottom;
String min=String.valueOf(minute);
if (minute<10) {
min="0"+String.valueOf(minute);
}
canvas.drawText(hour+":"+min,0f,y,mPaint);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
mPaint.setTextSize(mHourR*0.16f);
mPaint.setAlpha(255);
mPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(month+"."+day+" 星期"+getCnStr(dayOfWeek),0f,-mPaint.getFontMetrics().ascent,mPaint);
}
/**
* 数字转换字符
* @param number
* @return
*/
private String getCnStr(int number) {
String s = String.valueOf(number);
StringBuffer sb = new StringBuffer();
String result = "";
if (s.length()>1){
if (Integer.parseInt(String.valueOf(s.charAt(0)))!=1) {
result+=NUMBER_TEXT_LIST[Integer.parseInt(String.valueOf(s.charAt(0)))];
}
result += "十";
if (Integer.parseInt(String.valueOf(s.charAt(1)))>0) {
result +=NUMBER_TEXT_LIST[Integer.parseInt(String.valueOf(s.charAt(1)))];
}
} else {
result += NUMBER_TEXT_LIST[Integer.parseInt(String.valueOf(s.charAt(0)))];
}
return result;
}
}
}
showdowlayout 在页面中动态修改约束的时候会报错
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.