`
不夜的星辰
  • 浏览: 88342 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

温度曲线 抽屉

阅读更多
实现天气预报温度曲线绘制及抽屉效果显示:
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包裹
  • 大小: 23.5 KB
分享到:
评论

相关推荐

    智能小型回流焊机T-962A使用说明

    - 加工时,轻放物品于抽屉内,关闭抽屉,按F1启动,设备会按照选定的温度曲线自动执行。 - 在操作过程中,用户可实时监控液晶屏上的温度数据、曲线,根据需要修改参数。 - 回焊完成后,可通过F键强制停止或启动...

    无线无源温度检测原理.doc

    接收终端负责收集传感器数据并通过网络层传输到数据后台,后台则提供实时温度监控、历史曲线分析以及超温报警功能。 无线测温传感器采用美国TI公司的无线数字通信和低功耗技术,具有多重抗干扰措施和独特软件算法。...

    QN-800无铅全自动电脑控制大型八温区回流焊.pdf

    这种设计减少了传统小循环结构中因热风回流引起的温度波动,避免了焊接过程中对PCB板的温度曲线精度的影响,特别适合焊接空间窗口小的无铅元件。 设备的上下加热模组独立工作,拥有独立的热风循环系统,可以设置双...

    DW950安装使用说明书

    按照安装方式分类,则有固定式和抽屉式;脱扣器的种类分为智能型控制器、分励脱扣器、欠电压瞬时(或延时)脱扣器等。 此外,断路器按是否具有单相接地保护功能分为带单相接地保护和不带单相接地保护两种。智能控制...

    CA-600全自动血凝仪操作规程.doc

    当探测器达到适宜分析的温度,屏幕显示“Ready”,通常需要5-30分钟。 3. **工作前准备** - **试剂准备**: - 准备所需的凝集试剂如Owren’s Veronal缓冲液、洗涤溶液等。 - 每次测试的试剂消耗量:PT试剂100 μ...

    专题资料(2021-2022年)产品说明书下载1.doc

    该设备的一大特色是抽屉回路号码自动显示,方便操作。其内部集成两套嵌入式以太网控制器和1024级触发系统,实现全双工热备份工作,提高了系统的稳定性。调光柜支持网络远程设置和集中监控,遵循IEE802.3以太网协议,...

    施耐德Compact NS630B-1600A塑壳断路器产品简页.pdf.pdf

    4. 抽屉式设计:Compact NS630B-1600A具备真正的抽屉式设计,标配的安全挡板在本体摇出时能够实现主回路的隔离,提供了抽架位置指示及闭锁功能,为操作者提供了更直观和准确的指示。 5. 适用环境:它适用于温度高达...

    简析JL-200型电机保护器工作原理.pdf

    7. 采用模块化设计结构,产品体积小,结构紧凑,安装方便,在低压控制终端柜和1/4 模数及以上各种抽屉柜中可直接安装使用,提高了控制线路的可靠性和自动化水平。 8. 结构紧凑、华丽、精湛优美的外观和卓越的设计...

    K5矿用隔型软启动12.pdf

    - **可控软启动与软停止**:启动时间和停止时间可调,允许用户根据负载情况定制启动和停止曲线。 - **限制启动电流和转矩**:控制电机端电压,避免高启动电流和转矩对机械和电网的冲击。 - **自诊断与保护**:...

    宝临 BC60LE-63系列漏电断路器技术参数.rar

    压缩包中的“c.jpg”可能是产品手册或技术规格表的一部分,提供了更详细的数据图表,如电流-时间特性曲线、尺寸图等,有助于用户了解和选择适合的断路器。 总之,理解和掌握宝临 BC60LE-63系列漏电断路器的技术参数...

    精品专题(2021-2022年收藏)3C技术负责人考试题库.docx

    发热元件与周围元件之间保持足够的距离是为了确保散热良好,避免温度过高导致设备损坏或安全隐患。 22. **电力系统中TN-C系统是指电力系统中有一点直接接地,中性线和保护线是合一的。** - 正确。TN-C系统是一种...

    国外的创新思路.docx

    - **冷却功能水杯:** 内置制冷元件的保温杯,能够在较短时间内将饮料温度降至适宜饮用的温度。 #### Self-LockingBike **知识点:** - **自动锁定:** 一种配备了自动锁定系统的自行车,当无人使用时会自动锁定...

Global site tag (gtag.js) - Google Analytics