Implement the custom ImageView, TouchView.java.
package com.exercise.AndroidDetechTouch;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
public class TouchView extends ImageView {
public TouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public TouchView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TouchView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
float x = event.getX();
float y = event.getY();
((AndroidDetechTouchActivity)getContext()).updateMsg("Touched@" + x + " : " + y);
break;
case MotionEvent.ACTION_UP:
((AndroidDetechTouchActivity)getContext()).updateMsg("");
break;
}
return true;
}
}
Modify the main activity to add updateMsg() method. It will be called from custom ImageView.
package com.exercise.AndroidDetechTouch;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidDetechTouchActivity extends Activity {
TextView msg;
TouchView touchView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msg = (TextView)findViewById(R.id.msg);
touchView = (TouchView)findViewById(R.id.touchview);
}
public void updateMsg(String tMsg){
msg.setText(tMsg);
}
}
main.xml, to add the custom ImageView and a TextView overlapped.
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<com.exercise.AndroidDetechTouch.TouchView
android:id="@+id/touchview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/ic_launcher"
/>
</FrameLayout>
</LinearLayout>
Download the files.
Next: - Get pixel color on a specified location from ImageView's background bitmap
0 Response to "Detect touched position on a ImageView"
Post a Comment