Open top menu

 In this blog implement the finger feedback touch line draw on image view. the draw as a straight line . in this tutorial we will used Bitmap,Canvas, paint class.

Bitmap class- loading bitmap Objects and keep your UI.
Canvas class-with the help draw  a line and bitmap to hold pixels,
Paint class-holds style and color information.

first, you create activity_main.xml class inside res folder.
...............................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/layout">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/s" />

</LinearLayout>



MainActivity .java
................................................................................................................................................................

package com.androidbeginner.testcode;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnTouchListener {
 ImageView imageView;
 Bitmap mbitmap;
 Canvas mcanvas;
 Paint mpaint;
 float downaxies_x = 0, downaxies_y = 0, upaxies_X = 0, upaxies_y = 0;
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   imageView = (ImageView) this.findViewById(R.id.imageView1);

   Display currentDisplay = getWindowManager().getDefaultDisplay();
   float dwidth = currentDisplay.getWidth();
   float dhight = currentDisplay.getHeight();

   mbitmap = Bitmap.createBitmap((int) dwidth, (int) dhight,
       Bitmap.Config.ARGB_8888);
   mcanvas = new Canvas(mbitmap);
 
   mpaint = new Paint();
   mpaint.setColor(Color.RED);
   imageView.setImageBitmap(mbitmap);

   imageView.setOnTouchListener(this);
 }

 public boolean onTouch(View v, MotionEvent event) {
   int action = event.getAction();
   switch (action) {
   case MotionEvent.ACTION_DOWN:
    downaxies_x = event.getX();
    downaxies_y = event.getY();
     break;
   case MotionEvent.ACTION_MOVE:
     break;
   case MotionEvent.ACTION_UP:
    upaxies_X = event.getX();
    upaxies_y = event.getY();
       mcanvas.drawLine(downaxies_x, downaxies_y, upaxies_X, upaxies_y, mpaint);
     imageView.invalidate();
     break;
   case MotionEvent.ACTION_CANCEL:
     break;
   default:
     break;
   }
   return true;
 }
}

0 comments