Monday, 4 March 2013

Android Thermometer example using canvas

package com.example.canvassample;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();
    Heightwidth sample = new Heightwidth();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {

        int devicewidth = sample.getWidth();
        int deviceheight = sample.getHeight();
        int boxheight = deviceheight / 10;

        int Mwidht = devicewidth / 2;
        float rdistance = (float) (Mwidht / 1.2);
        float rdistancearc = (float) (Mwidht / 1.2);
        float ldistanve = (float) (1.3 * (rdistance));

        float rdistancesecond = rdistance + (Mwidht / 4);

        int top = deviceheight - boxheight;
        System.out.println("top is " + top);
        int toppos = top / 12;
        int botpos = 2 * (toppos);
        int botposarc = (toppos) + boxheight;

        // bottom arc
        int botarc = top / 5;
        int lastpara = boxheight + top;
        // canvas.drawLine(160, 500, 160, 160, paint);
        // canvas.drawLine(180, 500, 180, 160, paint);

        // canvas.drawLine(rdistance, 80, rdistance, botpos, paint);

        Paint p = new Paint();
        // smooths
        p.setAntiAlias(true);
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(1);
        RectF rect2 = new RectF(rdistance, toppos, ldistanve, botposarc);
        canvas.drawArc(rect2, 0, -180, false, p);

        // RectF rect3 = new RectF(rdistance,botarc,ldistanve,lastpara);
        // canvas.drawArc(rect3,0, 180, false, p);

        float center_x, center_y;
        center_x = devicewidth / 2;
        center_y = deviceheight / 4;
        float radius = deviceheight / 10;
        final RectF oval = new RectF();
       
        float parameter = boxheight;
       
        oval.set(center_x - radius, center_y - radius, center_x + radius,
                center_y + radius);

        paint.setStyle(Paint.Style.STROKE);
        center_x = devicewidth / 2;
        center_y = deviceheight * 3 / 4;
        oval.set(center_x - (radius+(radius/8)),
                (float) (center_y - (radius + (1.3 * boxheight))), center_x
                        + (radius-(radius/8)), center_y + (radius - (top / 5)));

       
        paint.setTextSize(28);
        //canvas.drawText("TemperatureLevel", Mwidht, 20, paint);

        canvas.drawArc(oval, -67, 315, false, paint);

        canvas.drawText("Hello", Mwidht - 20, (float) (deviceheight / 1.7), p);

        // canvas.drawPath(drawCurve(canvas, paint, rdistance, ldistanve),
        // paint);

        // canvas.drawArc(rect2, 270, 0, false, p);

        canvas.drawLine(rdistance, (float) (1.1 * boxheight), rdistance,
                (float) (5.25 * boxheight), paint);

        canvas.drawLine(rdistancesecond, (float) (1.1*boxheight),
                rdistancesecond, (float) (5.25 * boxheight), paint);
        // canvas.draw

        // right distance,length,right distance,todistance
        // canvas.drawLine(20, 0, 0, 20, paint);
    }

    private Path drawCurve(Canvas canvas, Paint paint, float rdistance,
            float ldistanve) {

        Path myPath = new Path();
        myPath.moveTo(rdistance, sample.getHeight() / 10);
        myPath.quadTo(rdistance, sample.getHeight() / 10, rdistance,
                sample.getHeight() / 10);
        return myPath;
    }
}



package com.example.canvassample;

public class Heightwidth {

    static int height;
    static int width;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }
}


package com.example.canvassample;

import android.app.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;

public class MainActivity extends Activity {
    DrawView drawView;
    Heightwidth sample = new Heightwidth();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String str_ScreenSize = "The Android Screen is: " + dm.widthPixels
                + " x " + dm.heightPixels;
        sample.setHeight(dm.heightPixels);
        sample.setWidth(dm.widthPixels);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

    }
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>


sampleoutput
----------------------

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...