Thursday, April 7, 2016

android - How to draw a Line on Canvas

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#b3bca5"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Draw A Line"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private Button mButton;
    private ImageView mImageView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mButton = (Button) findViewById(R.id.btn);
        mImageView = (ImageView) findViewById(R.id.iv);

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Create a new bitmap
                Bitmap bitmap = Bitmap.createBitmap(
                        600, // Width
                        400, // Height
                        Bitmap.Config.ARGB_8888 // Configuration
                );

                // Initialize a new Canvas using Bitmap
                Canvas canvas = new Canvas(bitmap);

                // Initialize a new Paint object
                Paint paint = new Paint();

                // Set the Paint color
                paint.setColor(Color.RED);

                // Set the Paint line width
                paint.setStrokeWidth(5.0f);

                // Draw a solid color on the Canvas
                canvas.drawColor(Color.parseColor("#FFE9F3FF"));

                // Draw a line on the Canvas
                canvas.drawLine(
                        50, // startX
                        50, // startY
                        500, // stopX
                        350, // stopY
                        paint // Paint object to draw
                );

                // Set the bitmap as ImageView image source
                mImageView.setImageBitmap(bitmap);
            }
        });
    }
}