实现天气预报温度曲线绘制及抽屉效果显示:
package com.canvas;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidCanvasActivity extends Activity {
private float tempLow[] = {10,24,15,23};
private float tempHigh[] = {33,40,33,34};
private float temp[] = {33,40,33,34,10,24,15,23};
private View view;
private LinearLayout llLayout,llLayout2;
private int screenWith,screenHeight;
float lowDis = 0,highDis = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// view = findViewById(R.id.view);
llLayout = (LinearLayout) findViewById(R.id.content);
llLayout2 = (LinearLayout) findViewById(R.id.ll);
Object object = getSystemService(WINDOW_SERVICE);
WindowManager wm = (WindowManager) object;
screenWith = wm.getDefaultDisplay().getWidth();
screenHeight = wm.getDefaultDisplay().getHeight();
llLayout.addView(new DrawView(this));
llLayout2.addView(new DrawView(this));
}
class DrawView1 extends SurfaceView implements SurfaceHolder.Callback, Runnable{
float circleX = screenWith / 16;//屏幕横向8等分
float circleY = 260.0f*screenHeight/480.0f;
private SurfaceHolder surfaceHolder;
public DrawView1(Context context) {
super(context);
surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
new Thread(this).start();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void run() {
while (!Thread.currentThread().interrupted()) {
synchronized (surfaceHolder) {
draw();
}
}
}
private void draw(){
Canvas canvas = null;
Matrix matrix = new Matrix();
if(!Thread.currentThread().interrupted()){
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLUE);
matrix.preTranslate(100f, 100f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
Paint linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setColor(Color.YELLOW);
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(8.0f);
for (int i = 0; i < tempLow.length; i++) {
canvas.drawCircle(circleX + i*circleX, tempLow[i]*5, 2, paint);
canvas.drawText(tempLow[i]+"°", circleX + i*circleX, tempLow[i]*5-10, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,tempLow[i]*5, circleX + (i+1)*circleX, tempLow[i+1]*5, linePaint);
}
}
for (int i = 0; i < tempHigh.length; i++) {
canvas.drawCircle(circleX + i*circleX, tempHigh[i]*5, 2, paint);
canvas.drawText(tempHigh[i]+"°", circleX + i*circleX, tempHigh[i]*5-10, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,tempHigh[i]*5, circleX + (i+1)*circleX, tempHigh[i+1]*5, linePaint);
}
}
}
canvas.setMatrix(matrix);
if(canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
class DrawView extends View{
float circleX = screenWith / 16;//屏幕横向8等分
float circleY = 260.0f*screenHeight/480.0f;
float[] sortData = sort(temp);
public DrawView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float highPx = 260.0f*screenHeight/480.0f;
float lowPx = 80.0f*screenHeight/480.0f;
float x0 = screenWith/16;//40.0f
float x1 = 3*x0;
float x2 = 5*x0;
float x3 = 7*x0;
//乘100再除100是未了保留精度
float y0 = highPx - (100*(tempHigh[0]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y1 = highPx - (100*(tempHigh[1]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y2 = highPx - (100*(tempHigh[2]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y3 = highPx - (100*(tempHigh[3]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y00 = highPx - (100*(tempLow[0]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y11 = highPx - (100*(tempLow[1]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y22 = highPx - (100*(tempLow[2]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y33 = highPx - (100*(tempLow[3]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
Paint linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setColor(Color.YELLOW);
linePaint.setTextSize(5.0f);
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(10.0f);
float pts1[] = {x0,y0,x1,y1,
x1,y1,x2,y2,
x2,y2,x3,y3
};
float pts2[] = {
x0,y00,x1,y11,
x1,y11,x2,y22,
x2,y22,x3,y33
};
linePaint.setColor(Color.YELLOW );;//
canvas.drawLines(pts1, linePaint);
linePaint.setColor(Color.GREEN);;//
canvas.drawLines(pts2, linePaint);
float radius = 5.0f;
// canvas.drawPoints(pts1, pointPaint);
paint.setColor(Color.WHITE);;//
canvas.drawCircle(x0, y0, radius, paint);
canvas.drawCircle(x1, y1, radius, paint);
canvas.drawCircle(x2, y2, radius, paint);
canvas.drawCircle(x3, y3, radius, paint);
// canvas.drawPoints(pts2, pointPaint);
paint.setColor(Color.WHITE );;//
canvas.drawCircle(x0, y00, radius, paint);
canvas.drawCircle(x1, y11, radius, paint);
canvas.drawCircle(x2, y22, radius, paint);
canvas.drawCircle(x3, y33, radius, paint);
float textWidth = 20.0f;
float textHeight1 = 10.0f*screenHeight/480.0f;
textPaint.setTextSize(20.0f);//
canvas.drawText(tempHigh[0]+"°", x0-textWidth/2, y0-textHeight1, textPaint);
canvas.drawText(tempHigh[1]+"°", x1-textWidth/2, y1-textHeight1, textPaint);
canvas.drawText(tempHigh[2]+"°", x2-textWidth/2, y2-textHeight1, textPaint);
canvas.drawText(tempHigh[3]+"°", x3-textWidth/2, y3-textHeight1, textPaint);
float textHeight2 = 25.0f;
textPaint.setTextSize(20.0f);//
canvas.drawText(tempLow[0]+"°", x0-textWidth/2, y00+textHeight2, textPaint);
canvas.drawText(tempLow[1]+"°", x1-textWidth/2, y11+textHeight2, textPaint);
canvas.drawText(tempLow[2]+"°", x2-textWidth/2, y22+textHeight2, textPaint);
canvas.drawText(tempLow[3]+"°", x3-textWidth/2, y33+textHeight2, textPaint);
float textWidth1 = 40.0f;
textPaint.setTextSize(20.0f);
if(screenHeight == 854){
lowDis = 630f;highDis = 645f;
}else if(screenHeight == 800){
lowDis = 580f;highDis = 595f;
}else if(screenHeight == 480){
lowDis = 290f;highDis = 270f;
}else if(screenHeight == 320){
lowDis = 150f;highDis = 125f;
}
/*for (int i = 0; i < tempLow.length; i++) {
canvas.drawCircle(circleX + i*circleX, screenHeight - tempLow[i]*5 - lowDis, 2, paint);
canvas.drawText(tempLow[i]+"°", circleX + i*circleX, screenHeight - tempLow[i]*5-10 - lowDis, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,screenHeight - tempLow[i]*5 - lowDis, circleX + (i+1)*circleX, screenHeight - tempLow[i+1]*5 - lowDis, linePaint);
}
}
for (int i = 0; i < tempHigh.length; i++) {
canvas.drawCircle(circleX + i*circleX, screenHeight - tempHigh[i]*5 - highDis, 2, paint);
canvas.drawText(tempHigh[i]+"°", circleX + i*circleX, screenHeight - tempHigh[i]*5-10 - highDis, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,screenHeight - tempHigh[i]*5 - highDis, circleX + (i+1)*circleX, screenHeight - tempHigh[i+1]*5 - highDis, linePaint);
}
}*/
}
}
private float[] sort(float[] data){
for (int i = 1; i < data.length; i++) { // 数组有多长,轮数就有多长
// 将相邻两个数进行比较,较大的数往后冒泡
for (int j = 0; j < data.length - i; j++) {// 每一轮下来会将比较的次数减少
if (data[j] > data[j + 1]) {
// 交换相邻两个数
swap(data, j, j + 1);
}
}
}
return data;
}
private void swap(float[] data, int x, int y) {
float temp = data[x];
data[x] = data[y];
data[y] = temp;
}
}
===========================main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绘图"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SlidingDrawer android:id="@+id/slidingDrawerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content">
<Button
android:id="@+id/handle"
android:layout_width="88dip"
android:layout_height="44dip"
android:background="@drawable/icon"
/>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
最外层需要用RelativeLayout包裹
package com.canvas;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidCanvasActivity extends Activity {
private float tempLow[] = {10,24,15,23};
private float tempHigh[] = {33,40,33,34};
private float temp[] = {33,40,33,34,10,24,15,23};
private View view;
private LinearLayout llLayout,llLayout2;
private int screenWith,screenHeight;
float lowDis = 0,highDis = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// view = findViewById(R.id.view);
llLayout = (LinearLayout) findViewById(R.id.content);
llLayout2 = (LinearLayout) findViewById(R.id.ll);
Object object = getSystemService(WINDOW_SERVICE);
WindowManager wm = (WindowManager) object;
screenWith = wm.getDefaultDisplay().getWidth();
screenHeight = wm.getDefaultDisplay().getHeight();
llLayout.addView(new DrawView(this));
llLayout2.addView(new DrawView(this));
}
class DrawView1 extends SurfaceView implements SurfaceHolder.Callback, Runnable{
float circleX = screenWith / 16;//屏幕横向8等分
float circleY = 260.0f*screenHeight/480.0f;
private SurfaceHolder surfaceHolder;
public DrawView1(Context context) {
super(context);
surfaceHolder = this.getHolder();
surfaceHolder.addCallback(this);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
new Thread(this).start();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void run() {
while (!Thread.currentThread().interrupted()) {
synchronized (surfaceHolder) {
draw();
}
}
}
private void draw(){
Canvas canvas = null;
Matrix matrix = new Matrix();
if(!Thread.currentThread().interrupted()){
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLUE);
matrix.preTranslate(100f, 100f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
Paint linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setColor(Color.YELLOW);
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(8.0f);
for (int i = 0; i < tempLow.length; i++) {
canvas.drawCircle(circleX + i*circleX, tempLow[i]*5, 2, paint);
canvas.drawText(tempLow[i]+"°", circleX + i*circleX, tempLow[i]*5-10, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,tempLow[i]*5, circleX + (i+1)*circleX, tempLow[i+1]*5, linePaint);
}
}
for (int i = 0; i < tempHigh.length; i++) {
canvas.drawCircle(circleX + i*circleX, tempHigh[i]*5, 2, paint);
canvas.drawText(tempHigh[i]+"°", circleX + i*circleX, tempHigh[i]*5-10, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,tempHigh[i]*5, circleX + (i+1)*circleX, tempHigh[i+1]*5, linePaint);
}
}
}
canvas.setMatrix(matrix);
if(canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
class DrawView extends View{
float circleX = screenWith / 16;//屏幕横向8等分
float circleY = 260.0f*screenHeight/480.0f;
float[] sortData = sort(temp);
public DrawView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float highPx = 260.0f*screenHeight/480.0f;
float lowPx = 80.0f*screenHeight/480.0f;
float x0 = screenWith/16;//40.0f
float x1 = 3*x0;
float x2 = 5*x0;
float x3 = 7*x0;
//乘100再除100是未了保留精度
float y0 = highPx - (100*(tempHigh[0]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y1 = highPx - (100*(tempHigh[1]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y2 = highPx - (100*(tempHigh[2]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y3 = highPx - (100*(tempHigh[3]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y00 = highPx - (100*(tempLow[0]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y11 = highPx - (100*(tempLow[1]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y22 = highPx - (100*(tempLow[2]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
float y33 = highPx - (100*(tempLow[3]-sortData[0])/(sortData[7]-sortData[0])*(highPx-lowPx)/100);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
Paint linePaint = new Paint();
linePaint.setAntiAlias(true);
linePaint.setColor(Color.YELLOW);
linePaint.setTextSize(5.0f);
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Color.WHITE);
textPaint.setTextSize(10.0f);
float pts1[] = {x0,y0,x1,y1,
x1,y1,x2,y2,
x2,y2,x3,y3
};
float pts2[] = {
x0,y00,x1,y11,
x1,y11,x2,y22,
x2,y22,x3,y33
};
linePaint.setColor(Color.YELLOW );;//
canvas.drawLines(pts1, linePaint);
linePaint.setColor(Color.GREEN);;//
canvas.drawLines(pts2, linePaint);
float radius = 5.0f;
// canvas.drawPoints(pts1, pointPaint);
paint.setColor(Color.WHITE);;//
canvas.drawCircle(x0, y0, radius, paint);
canvas.drawCircle(x1, y1, radius, paint);
canvas.drawCircle(x2, y2, radius, paint);
canvas.drawCircle(x3, y3, radius, paint);
// canvas.drawPoints(pts2, pointPaint);
paint.setColor(Color.WHITE );;//
canvas.drawCircle(x0, y00, radius, paint);
canvas.drawCircle(x1, y11, radius, paint);
canvas.drawCircle(x2, y22, radius, paint);
canvas.drawCircle(x3, y33, radius, paint);
float textWidth = 20.0f;
float textHeight1 = 10.0f*screenHeight/480.0f;
textPaint.setTextSize(20.0f);//
canvas.drawText(tempHigh[0]+"°", x0-textWidth/2, y0-textHeight1, textPaint);
canvas.drawText(tempHigh[1]+"°", x1-textWidth/2, y1-textHeight1, textPaint);
canvas.drawText(tempHigh[2]+"°", x2-textWidth/2, y2-textHeight1, textPaint);
canvas.drawText(tempHigh[3]+"°", x3-textWidth/2, y3-textHeight1, textPaint);
float textHeight2 = 25.0f;
textPaint.setTextSize(20.0f);//
canvas.drawText(tempLow[0]+"°", x0-textWidth/2, y00+textHeight2, textPaint);
canvas.drawText(tempLow[1]+"°", x1-textWidth/2, y11+textHeight2, textPaint);
canvas.drawText(tempLow[2]+"°", x2-textWidth/2, y22+textHeight2, textPaint);
canvas.drawText(tempLow[3]+"°", x3-textWidth/2, y33+textHeight2, textPaint);
float textWidth1 = 40.0f;
textPaint.setTextSize(20.0f);
if(screenHeight == 854){
lowDis = 630f;highDis = 645f;
}else if(screenHeight == 800){
lowDis = 580f;highDis = 595f;
}else if(screenHeight == 480){
lowDis = 290f;highDis = 270f;
}else if(screenHeight == 320){
lowDis = 150f;highDis = 125f;
}
/*for (int i = 0; i < tempLow.length; i++) {
canvas.drawCircle(circleX + i*circleX, screenHeight - tempLow[i]*5 - lowDis, 2, paint);
canvas.drawText(tempLow[i]+"°", circleX + i*circleX, screenHeight - tempLow[i]*5-10 - lowDis, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,screenHeight - tempLow[i]*5 - lowDis, circleX + (i+1)*circleX, screenHeight - tempLow[i+1]*5 - lowDis, linePaint);
}
}
for (int i = 0; i < tempHigh.length; i++) {
canvas.drawCircle(circleX + i*circleX, screenHeight - tempHigh[i]*5 - highDis, 2, paint);
canvas.drawText(tempHigh[i]+"°", circleX + i*circleX, screenHeight - tempHigh[i]*5-10 - highDis, textPaint);
if(i < tempLow.length - 1){
canvas.drawLine(circleX + i*circleX,screenHeight - tempHigh[i]*5 - highDis, circleX + (i+1)*circleX, screenHeight - tempHigh[i+1]*5 - highDis, linePaint);
}
}*/
}
}
private float[] sort(float[] data){
for (int i = 1; i < data.length; i++) { // 数组有多长,轮数就有多长
// 将相邻两个数进行比较,较大的数往后冒泡
for (int j = 0; j < data.length - i; j++) {// 每一轮下来会将比较的次数减少
if (data[j] > data[j + 1]) {
// 交换相邻两个数
swap(data, j, j + 1);
}
}
}
return data;
}
private void swap(float[] data, int x, int y) {
float temp = data[x];
data[x] = data[y];
data[y] = temp;
}
}
===========================main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/ll">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绘图"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<SlidingDrawer android:id="@+id/slidingDrawerId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:handle="@+id/handle"
android:content="@+id/content">
<Button
android:id="@+id/handle"
android:layout_width="88dip"
android:layout_height="44dip"
android:background="@drawable/icon"
/>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
最外层需要用RelativeLayout包裹