Project Structure :
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.rajeshvijayakumar.view.CustomViewEx
android:id="@+id/custom_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.rajeshvijayakumar.view.CustomViewEx
android:id="@+id/custom_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
CustomViewEx.java
package com.rajeshvijayakumar.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomViewEx extends View {
private PointF point;
private Paint paint;
public CustomViewEx(Context context) {
this(context, null);
}
public CustomViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
if (point != null) {
int radius = 50;
paint.setColor(Color.GREEN);
canvas.drawCircle(point.x, point.y, radius, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
point = new PointF(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
point = null;
break;
case MotionEvent.ACTION_MOVE:
point = new PointF(event.getX(), event.getY());
break;
}
invalidate();
return true;
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class CustomViewEx extends View {
private PointF point;
private Paint paint;
public CustomViewEx(Context context) {
this(context, null);
}
public CustomViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
if (point != null) {
int radius = 50;
paint.setColor(Color.GREEN);
canvas.drawCircle(point.x, point.y, radius, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
point = new PointF(event.getX(), event.getY());
break;
case MotionEvent.ACTION_UP:
point = null;
break;
case MotionEvent.ACTION_MOVE:
point = new PointF(event.getX(), event.getY());
break;
}
invalidate();
return true;
}
}
MainActivity.java
package com.example.custom_view_example;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.custom_view_example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.custom_view_example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.custom_view_example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.custom_view_example.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output :
Source Code : Download this Example Here
thanks for your reply...but i have to paint inside the rotated rectangle only in canvas....
ReplyDeletethanks pal, a very simple and good example..
ReplyDelete