Hi Friends,
Once i was working on a project where in app i have to place marker on the static ImageView programmatically onTouchEvent, I Google it but not getting any informative inform from Google then i decide to try this myself, its take my 2 day to complete this task, i know if anyone struck when he/she are at the end of any project that is really frustrating, that's why i am going to share this information with you people hope this will solve your problems:
Example:
Codes:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<in.androiddevelopmentanddiscussion.markeronimageview.PaintSurface
android:id="@+id/paint_surface"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Where is California (State)"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
MainActivity.java
package in.androiddevelopmentanddiscussion.markeronimageview;
import in.androiddevelopmentanddiscussion.drawandcanvasexample.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.SurfaceHolder;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
PaintSurface mPaintSurface;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPaintSurface = (PaintSurface)findViewById(R.id.paint_surface);
mPaintSurface.setZOrderOnTop(true);
SurfaceHolder sh = mPaintSurface.getHolder();
sh.setFormat(PixelFormat.TRANSLUCENT);
mPaintSurface.setOnTouchListener(mPaintSurface);
}
@Override
protected void onResume() {
super.onResume();
mPaintSurface.resume();
}
@Override
protected void onPause() {
super.onPause();
mPaintSurface.pause();
}
public static void alertMsg(final Context context,String msg){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Result");
alertDialogBuilder
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intent = ((Activity) context).getIntent();
((Activity) context).overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
((Activity) context).finish();
((Activity) context).overridePendingTransition(0, 0);
((Activity) context).startActivity(intent);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.androiddevelopmentanddiscussion.drawandcanvasexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="in.androiddevelopmentanddiscussion.markeronimageview.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>
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
Once i was working on a project where in app i have to place marker on the static ImageView programmatically onTouchEvent, I Google it but not getting any informative inform from Google then i decide to try this myself, its take my 2 day to complete this task, i know if anyone struck when he/she are at the end of any project that is really frustrating, that's why i am going to share this information with you people hope this will solve your problems:
Example:
Codes:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<in.androiddevelopmentanddiscussion.markeronimageview.PaintSurface
android:id="@+id/paint_surface"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Where is California (State)"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
MainActivity.java
package in.androiddevelopmentanddiscussion.markeronimageview;
import in.androiddevelopmentanddiscussion.drawandcanvasexample.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.SurfaceHolder;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView iv;
PaintSurface mPaintSurface;
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPaintSurface = (PaintSurface)findViewById(R.id.paint_surface);
mPaintSurface.setZOrderOnTop(true);
SurfaceHolder sh = mPaintSurface.getHolder();
sh.setFormat(PixelFormat.TRANSLUCENT);
mPaintSurface.setOnTouchListener(mPaintSurface);
}
@Override
protected void onResume() {
super.onResume();
mPaintSurface.resume();
}
@Override
protected void onPause() {
super.onPause();
mPaintSurface.pause();
}
public static void alertMsg(final Context context,String msg){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Result");
alertDialogBuilder
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Intent intent = ((Activity) context).getIntent();
((Activity) context).overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
((Activity) context).finish();
((Activity) context).overridePendingTransition(0, 0);
((Activity) context).startActivity(intent);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
PaintSurface.java
package in.androiddevelopmentanddiscussion.markeronimageview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import in.androiddevelopmentanddiscussion.drawandcanvasexample.R;
import in.androiddevelopmentanddiscussion.markeronimageview.MainActivity;
public class PaintSurface extends SurfaceView implements Runnable, OnTouchListener{
private SurfaceHolder mHolder;
private Thread mThread;
private boolean mFlag = false;
private float mX;
private float mY;
private Paint mPaint,nPaint,lPaint;
Canvas canvas;
float distance;
public PaintSurface(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.usa);
mHolder = getHolder();
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
nPaint = new Paint();
nPaint.setColor(Color.RED);
lPaint = new Paint();
lPaint.setColor(Color.BLUE);
}
public void resume(){
// Instantiating the thread
mThread = new Thread(this);
mFlag = true;
mThread.start();
}
public void pause(){
mFlag = false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mX = event.getX();
mY = event.getY();
distance = (float) Math.sqrt(Math.pow(101 - mX, 2) + Math.pow(280 - mY, 2));
String msg = "X axis: "+mX+"\nY axis: "+mY+"\nDistance :"+distance;
MainActivity.alertMsg(getContext(),msg);
break;
}
return true;
}
@Override
public void run() {
while(mFlag){
if(!mHolder.getSurface().isValid())
continue;
canvas = mHolder.lockCanvas();
canvas.drawARGB(0, 0, 0, 0);
if(distance<=0){
}else if(distance<50){
Bitmap bmp3 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp3, 101- (bmp3.getWidth() / 3), 280- (bmp3.getHeight() / 3), null);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp2, mX- (bmp2.getWidth() / 3), mY- (bmp2.getHeight() / 3), null);
}else{
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp1, 101- (bmp1.getWidth() / 3), 280- (bmp1.getHeight() / 3), null);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.red_flag);
canvas.drawBitmap(bmp, mX- (bmp.getWidth() / 3), mY- (bmp.getHeight() / 3), null);
canvas.drawLine(101 , 280,mX, mY, lPaint);
}
mHolder.unlockCanvasAndPost(canvas);
}
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.androiddevelopmentanddiscussion.drawandcanvasexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="in.androiddevelopmentanddiscussion.markeronimageview.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>
Download Full Project From Here :
Check Android Apps on Google Play
https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en
No comments:
Post a Comment