Touch Scroll on View Flipper in Android? - android

I have to achieve that the Touch Scroll on the ViewFlipper. For Example. I have two Images. At First, ViewFlipper shows an First Image. Now I Flung the view from right to left. The First Image view Slide out left and the Second Slide in from Left. I can achieve it By this Post. But I want to Scroll the image. That is, on the Action_Move Event I want to do Touch Scroll. For Example, when I move the touch from right to left it will flung how much the touch moves. on that time the output should show both images partly.
How to do that? What I have to measure the Screen levels(height & width). Example codes are more helpful.

If you need to detect scroll on only viewflipper which is not occupying entire screen, then try the below
gestureDetector = new GestureDetector(new MyGestureDetector());
viewFlipper.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return false;
}
return true;
}
});
and MyGestureDetector will be same as in http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/

package com.appaapps.flipper;
import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
//------------------------------------------------------------------------------
// Flipper - Philip R Brenan at gmail.com
//------------------------------------------------------------------------------
public class FlipperActivity extends Activity {
ViewFlipper f;
DrawView a, b, c;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
f = new ViewFlipper(this);
a = new DrawView(this, "aaaaa");
b = new DrawView(this, "BBBBB");
c = new DrawView(this, "ccccc");
f.addView(a);
f.addView(b);
f.addView(c);
setContentView(f);
}
//------------------------------------------------------------------------------
// Draw
//------------------------------------------------------------------------------
class DrawView extends View implements View.OnTouchListener {
final String text;
DrawView(Context Context, String Text) {
super(Context);
text = Text;
setOnTouchListener(this);
}
public void onDraw(Canvas Canvas) {
super.onDraw(Canvas);
Paint p = new Paint();
p.setColor(0xffffffff);
p.setTextSize(20);
Canvas.drawText(text, 0, 20, p);
}
public boolean onTouch(View v, MotionEvent event) {
final int a = event.getAction();
if (a == MotionEvent.ACTION_DOWN) {
final int i = f.getDisplayedChild(), n = f.getChildCount();
f.setDisplayedChild((i + 1) % n);
}
return true;
}
}
}

Related

Draw new Circle with each new Touch Event

I have a class that draws a circle at the point where the user touches. However, the circle disappears whenever a new point on the same surface is touched. I would like to keep the circle and draw a new one instead. Here is my current code.
import android.graphics.Canvas;
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 java.util.Random;
public class ArtSurface extends SurfaceView implements Runnable, View.OnTouchListener {
private SurfaceHolder aHolder;
private Thread aThread;
private boolean aFlag=false;
private float aX;
private float aY;
private Paint aPaint;
private Random cRandom;
public int randomColor(){
if (cRandom==null){
cRandom=new Random();
}
int randomCol=0xff000000+256*256*cRandom.nextInt(256)+256*cRandom.nextInt(256)+cRandom.nextInt(256);
return randomCol;
}
public ArtSurface(Context context, AttributeSet attrs) {
super(context, attrs);
aHolder=getHolder();
aX=-100;
aY=-100;
aPaint=new Paint();
aPaint.setColor(randomColor());
}
public void resume(){
aThread= new Thread(this);
aFlag=true;
aThread.start();
}
public void pause(){
aFlag=false;
}
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
aX= motionEvent.getX();
aY=motionEvent.getY();
break;
case MotionEvent.ACTION_UP:
//Leave art where it is
break;
}
return true;
}
#Override
public void run() {
while(aFlag){
if(!aHolder.getSurface().isValid())
continue;
Canvas canvas=aHolder.lockCanvas();
canvas.drawARGB(255,255,255,255);
canvas.drawCircle(aX,aY,50,aPaint);
aHolder.unlockCanvasAndPost(canvas);
}
}
}```
The way the code is currently, every new touch event causes a new circle to be drawn at the new point of touch. The previous circle is removed and I can only draw one circle at a time. My main aim is to have a new circle with a random color whenever the user touches a new point on the screen. All the other previous circles should remain where they were initially rendered. Thank you.
The line
canvas.drawARGB(255,255,255,255);
keeps drawing over the rendered circle. Removing it means that each new circle is visible on the canvas.

why is this setOnTouchListener for SurfaceView but onTouch for Activity

So I have followed this tutorial and the code works perfectly. However I have some trouble understanding how OnTouchListener and OnTouch work together. I have spent a long time trauling this forum, websites and documentation to understand but still, I do not.
In this code, a OnTouchListener is set for ourSurfaceView, and then the onTouch is called for the activity?!
Can someone please explain the relationship of OnTouchListener and OnTouch across different activities and views. Many Thanks!
package com.games.michael.waterproofme;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener{
MySurface ourSurfaceView;
float x,y,sX, sY, fX, fY;
Bitmap test, plus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourSurfaceView = new MySurface(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fX = 0;
fY = 0;
test = BitmapFactory.decodeResource(getResources(), R.drawable.sportsball);//draw ball
plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus);
setContentView(ourSurfaceView);
}
#Override
protected void onPause() {
super.onPause();
ourSurfaceView.pause();
}
#Override
protected void onResume() {
super.onResume();
ourSurfaceView.resume();
}
#Override
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
sX = event.getX();
sY = event.getY();
break;
case MotionEvent.ACTION_UP:
fX = event.getX();
fY = event.getY();
break;
}
return true;//false = finished dont loop through. true = loop through
}
public class MySurface extends SurfaceView implements Runnable{
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MySurface(Context context){
super(context);
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
}
public void pause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume(){
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
public void run() {
while(isRunning){
if(!ourHolder.getSurface().isValid()) {/
continue;}
Canvas canvas = ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150);
if (x != 0 && y != 0){
canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);//bitmap, left, top, paint
}
if (sX != 0 && sY != 0){
canvas.drawBitmap(plus, sX-(plus.getWidth()/2), sY-(plus.getHeight()/2), null);//bitmap, left, top, paint
}
if (fX != 0 && fY != 0){
canvas.drawBitmap(plus, fX-(plus.getWidth()/2), fY-(plus.getHeight()/2), null);//bitmap, left, top, paint
}
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
OnTouchListener is interface - class that implements it must override its methods. For android.view.View.OnTouchListener this is one method: boolean onTouch(View v, MotionEvent event)
When touch event occurs in your SurfaceView there is check if onTouchListener is set and if so its onTouch method is called
Can someone please explain the relationship of OnTouchListener and OnTouch across different activities and views.
Touch events on views are invoked if you register any callback to them. SurfaceView extends the View class.
ourSurfaceView.setOnTouchListener(this);
setContentView(ourSurfaceView);
So you just set the ourSurfaceView to the activity as their content view and registered the View.OnTouchListener. That means the abstract method onTouch is invoked on the ourSurfaceView instance and not the activity.
In this code, a OnTouchListener is set for ourSurfaceView, and then the onTouch is called for the activity?!
No, it delegates any touch event from ourSurfaceView instance as their content view to your MainActivity class because you registered the View.OnTouchListener
Just a simple Java example:
public class Main {
public static void main(String[] args){
SurfaceClass surfaceClass = new SurfaceClass();
ActivityClass activityClass = new ActivityClass();
surfaceClass.setOnFartListener(activityClass);
//Touch event :D
surfaceClass.fart();
}
public static class ActivityClass implements SurfaceClass.OnFartListener{
#Override
public void onFart(String kindOfFart) {
System.out.println(kindOfFart);
}
}
public static class SurfaceClass{
private SurfaceClass.OnFartListener onFartListener;
public void fart(){
if(onFartListener != null){
onFartListener.onFart("Huge One!!");
}
}
public void setOnFartListener(SurfaceClass.OnFartListener onFartListener){
this.onFartListener = onFartListener;
}
public interface OnFartListener{
void onFart(String kindOfFart);
}
}}
See touch event below :D
javac Main.java && java Main

android drag and drop ImageView onTouchListener

Hello I've set up an onTouchListener within my application for an ImageView, my aim was to have an ImageView that the user would be able to drag around and place where ever they want within the app. I've written some code using sample code found on the web for an onTouchListener that I thought would work but I'm having some problems with it, rather than allowing me to drag the image around, the image resizes and gets bigger or smaller when you drag your finger over it.
Does anyone have any ideas why? Here's my code:
ImageView t1img;
t1img = (ImageView) findViewById(R.id.imgT1);
windowWidth = getWindowManager().getDefaultDisplay().getWidth();
windowHeight = getWindowManager().getDefaultDisplay().getHeight();
t1img.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (LayoutParams) t1img.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int xCoord = (int) event.getRawX();
int yCoord = (int) event.getRawY();
if (xCoord > windowWidth) {
xCoord = windowWidth;
}
if (yCoord > windowHeight) {
yCoord = windowHeight;
}
layoutParams.leftMargin = xCoord - 25;
layoutParams.topMargin = yCoord - 75;
t1img.setLayoutParams(layoutParams);
break;
default:
break;
}
return true;
}
});
If you need to support gingerbread you can take a look at my example here
https://github.com/NAYOSO/android-dragview
if you only need to support jelly bean above you can use the Drag and Drop from android library you can see it from this article
http://developer.android.com/guide/topics/ui/drag-drop.html
For some explanation about the Drag and Drop view
at first you need t create the touch listener and then call startDrag to start draging. As simple as that.
private final class dragTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
return true;
} else {
return false;
}
}
}
To monitor the target of dropping place you can use onDragListener
private class dropListener implements OnDragListener {
View draggedView;
CustomTextView dropped;
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
draggedView = (View) event.getLocalState();
dropped = (CustomTextView) draggedView;
draggedView.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
CustomTextView dropTarget = (CustomTextView) v;
dropTarget.setText(dropped.getText().toString());
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
default:
break;
}
return true;
}
}
As you can see from my code there is many event but the main one is when the view is start being dragged, dropped and ended.
Don't forget to set the listener to view
tvDrag.setOnTouchListener(new dragTouchListener());
tvDrop.setOnDragListener(new dropListener())
I hope my explanation is clear enough!
If you have further question I will try to answer it tonight or tomorrow :)
I have done an example using thread. You can try this example for a smooth drag and drop bitmap image using touch Event listener
Download Demo or Code
I have created three classes
MainActivity.Java
Instatiates our BallView class and Adds setContentView to the BallView Class as shown below
package com.whatsonline.dragobject;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
public class MainActivity extends Activity {
private Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int w=getWindowManager().getDefaultDisplay().getWidth()-25;
int h=getWindowManager().getDefaultDisplay().getHeight()-25;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
BallView ballView=new BallView(this, bitmap, w, h);
setContentView(ballView);
}
}
Our BallView extends the surfaceView class and implements the surface holder as shown below. Also, contains the draw method to draw the canvas and touchEvent listener to get the position of the Bitmap image to be moved and move along the drawn image Bitmap path.
package com.whatsonline.dragobject;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BallView extends SurfaceView implements SurfaceHolder.Callback {
private Bitmap bitmap;
private MyThread thread;
private int x=25,y=25;
int width,height;
public BallView(Context context, Bitmap bmp, int w,int h) {
super(context);
width=w;
height=h;
bitmap=bmp;
thread=new MyThread(getHolder(),this);
getHolder().addCallback(this);
setFocusable(true);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// bitmap = BitmapFactory.decodeResource(getResources(), bmp);
canvas.drawColor(Color.BLUE);//To make background
canvas.drawBitmap(bitmap, x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2), null);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
x=(int)event.getX();
y=(int)event.getY();
if(x<25)
x=25;
if(x> width)
x=width;
if(y <25)
y=25;
if(y > height)
y=height;
return true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
thread.startrun(true);
thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread.startrun(false);
thread.stop();
}
}
and finally
MainThread class
call the onDraw method if it is running as shown below
package com.whatsonline.dragobject;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
public class MyThread extends Thread {
private SurfaceHolder msurfaceHolder;
private BallView mballView;
private boolean mrun =false;
public MyThread(SurfaceHolder holder, BallView ballView) {
msurfaceHolder = holder;
mballView=ballView;
}
public void startrun(boolean run) {
mrun=run;
}
#Override
public void run() {
super.run();
Canvas canvas;
while (mrun) {
canvas=null;
try {
canvas = msurfaceHolder.lockCanvas(null);
synchronized (msurfaceHolder) {
mballView.onDraw(canvas);
}
} finally {
if (canvas != null) {
msurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}
}
Download code here

(Android) Making Images Visible/Not Visible when touched

This is my updated code. It doesn't detect movement at all now. Maybe I shouldn't be making each Image an instance? Basically I want to user to be able to swipe through all the images to make them dissapear.
Thanks for all the help.
package com.picomputing.mythirdapplication;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Paul on 8/13/13.
*/
public class Pin extends ImageView implements View.OnTouchListener {
boolean isPinDown;
public Pin(Context context) {
super(context);
this.isPinDown = false;
}
public Pin(Context context, AttributeSet attrs) {
super(context, attrs);
this.isPinDown = false;
}
public Pin(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.isPinDown = false;
}
public boolean pinDown() {
return this.isPinDown;
}
public void setPinDown() {
this.isPinDown = true;
}
public void setPinUp() {
this.isPinDown = false;
}
public void togglePin() {
if (isPinDown == false)
{
isPinDown = true;
this.setImageResource(Color.TRANSPARENT);
}
else
{
isPinDown = false;
this.setImageResource(R.drawable.pin);
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX(); //--relative to mLayout--
int y = (int) event.getY(); //--relative to mLayout--
Rect r = new Rect();
view.getHitRect(r);
if(r.contains(x,y) && view instanceof ImageView){
togglePin();
}
}
return true;
}
}
You need to listen and consume ACTION_MOVE events, for the parent view of whatever you are trying to change.
Here's an example with a couple of ImageViews in a LinerLayout as a parent:
public class test extends Activity {
LinearLayout mLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLayout = new LinearLayout(this);
mLayout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0 ; i < 5; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(android.R.drawable.ic_dialog_info);
mLayout.addView(iv);
}
setContentView(mLayout);
mLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_MOVE:
int x = (int) event.getX(); //--relative to mLayout--
int y = (int) event.getY(); //--relative to mLayout--
Rect r = new Rect();
for(int i = 0 ; i < mLayout.getChildCount(); i++){
View v = mLayout.getChildAt(i);
v.getHitRect(r);
if(r.contains(x,y) && v instanceof ImageView){
((ImageView) v).setImageResource(android.R.drawable.ic_dialog_alert);
}
}
}
return true; //-- this means that view is interested in more events of all kinds--
}
});
}
}
I hope I didn't misunderstand your question
but if what you want to do is to prevent multitoch on the image you can add this attribute
android:splitMotionEvents="false"
in the xml in the parent view of the imageview. for example :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:splitMotionEvents="false"
>
// YOUR IMAGE VIEW HERE
</LinearLayout>
if you have any question feel free to ask in the comment :)
there are mainly three events on OnTouch action_down,Action_move and Action_up. do your coding on action down event i.e when user has touched your view. see the example here:
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
//write your code here
}
else {
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
do things
}
else {
if (arg1.getAction()==MotionEvent.ACTION_UP){
do things
}
}
}

how to move an imageview to right get activity in android?

I have implemented an application with image view in my application i would like to implement when user move the image right side then i would like to get the next acitvity.
How can i write an event or any other code for move the image just right side?
I have used am imageView code as:
(ImageView)findViewById(R.id.slide) //event for move right here
Intent it = new Intent(Slide.this,NextActivity.class);
startActivity(it);
Please any body help me...
So, if i understood you correctly, you want to move an ImageView across the screen, drag it, and when you hit a certain spot with it you want to start another activity?
If so, you could try and use the code below...which let's you drag your imageview across the screen
Just add an onTouchListener to the imageView you're trying to slide (move/drag)
yourSlideImageViewObject.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
onSlideTouch(v, event);
return false;
}
});
Then add this code to handle the Touch events
public void onSlideTouch( View view, MotionEvent event )
{
//When the user pushes down on an ImageView
if ( event.getAction() == MotionEvent.ACTION_DOWN )
{
inDragMode = true; //Set a variable so we know we started draggin the imageView
//Set the selected ImageView X and Y exact position
selectedImageViewX = Math.abs((int)event.getRawX()-((ImageView)view).getLeft());
selectedImageViewY = Math.abs((int)event.getRawY()-((ImageView)view).getTop());
//Bring the imageView in front
((ImageView)view).bringToFront();
}
//When the user let's the ImageView go (raises finger)
if ( event.getAction() == MotionEvent.ACTION_UP )
{
inDragMode = false; //Reset the variable which let's us know we're not in drag mode anymore
}
//When the user keeps his finger on te screen and drags it (slides it)
if ( event.getAction() == MotionEvent.ACTION_MOVE )
{
//If we've started draggin the imageView
if ( inDragMode )
{
//Get the imageView object
ImageView slide = (ImageView)findViewById(R.id.slide);
//Get a parameters object (THIS EXAMPLE IS FOR A RELATIVE LAYOUT)
RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams)slide.getLayoutParams();
//Change the position of the imageview accordingly
params.setMargins((int)event.getRawX()-selectedImageViewX, (int)event.getRawY()-selectedImageViewY, 0, 0);
//Set the new params
slide.setLayoutParams(params);
//If we hit a limit with our imageView position
if( slideImageView_position is inside an interval )
{
//Open another activity
Intent it = new Intent(Slide.this,NextActivity.class);
startActivity(it);
}
}
}
}
The above should drag your imageview accross the screen.
You can also implement in the MotionEvent.ACTION_MOVE event .... to limit the movement of the ImageView accross the screen. So check the new coordinates of your finger across the screen and check if they are out of bounds. If so, then do nothing. If they aren't out of bounds, then change the left and upper margins of the ImaveView.
Also, on the MotionEvent.ACTION_UP event, check to see if the left margin of the ImaveView is over a predefined limit (which means the user slided the imageView over you intended position). If so, you can continue doing whatever you want, which means starting an activity. OR, if you don't want to open the activity while the user is dragging, not only after he has removed his finger from the imageView, then just do that if () i've written under the MotionEvent.ACTION_MOVE event. Insert there the interval you want, so when the imageView is dragged inside that interval, the activity starts.
If you're confused, please ask :) It's very late here, and i'm tired, so i might have not explained it properly :D
I don't quite understand your question, but I interpret it as that you want to slide an ImageView containing Activity into another Activity.
If that is the case, I would actually convert those Activities to being Fragments and then use a ViewPager. Here is a good Google Developer blog post that introduces this concept: http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
You'll want to implement a FragmentPagerAdapter with getCount being the number of pages the user can swipe between. Then getItem should return a Fragment containing the first ImageView for position 0 and a Fragment containing what is in your NextActivity for position 1. You can return more Fragments for positions 2 and higher to have an interface that you can swipe between, left and right. This hopefully gives you the sliding user experience that you are looking for.
package avatar.view.view;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class customview extends View {
private int PanX, PanY;
private GestureDetector gestureDetector;
private Bitmap mBitmap;
private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
public customview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO 自動生成されたコンストラクター・スタブ
}
public customview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO 自動生成されたコンストラクター・スタブs
}
public customview(Context context) {
super(context);
// TODO 自動生成されたコンストラクター・スタブ
}
private float pointX = 0, pointY = 0;
private void setPoint(MotionEvent e) {
pointX = e.getX();
pointY = e.getY();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null) {
pointX = PanX - (mBitmap.getWidth() / 2);
pointY = PanY - (mBitmap.getHeight() / 2);
canvas.drawBitmap(mBitmap, pointX, pointY, mPaint);
}
}
/*
* (非 Javadoc)
*
* #see android.view.View#onMeasure(int, int)
*/
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// ビューのサイズを設定する
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec
.getSize(heightMeasureSpec));
}
public void setPanX(int panX) {
PanX = panX;
}
public int getPanX() {
return PanX;
}
public void setPanY(int panY) {
PanY = panY;
}
public int getPanY() {
return PanY;
}
public void setmBitmap(Bitmap mBitmap) {
this.mBitmap = mBitmap;
}
public Bitmap getmBitmap() {
return mBitmap;
}
}
Custom a view . Then in Main Activity
#Override
public boolean onTouch(View v, MotionEvent e) {
// TODO 自動生成されたメソッド・スタブ
if (isSelected == false) {
return false;
}
final int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
panX = (int) e.getX();
panY = (int) e.getY();
ctview.setPanX(panX);
ctview.setPanY(panY);
ctview.invalidate();
Log.v("ACTION_DOWN", "ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
panX = (int) e.getX();
panY = (int) e.getY();
ctview.setPanX(panX);
ctview.setPanY(panY);
ctview.invalidate();
Log.v("ACTION_MOVE", "ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.v("ACTION_UP", "ACTION_UP");
if (isSelected) {
isSelected = false;
ctview.setmBitmap(null);
ctview.invalidate();
}
break;
default:
break;
}
return true;
}
And Oncreate
Customview.setmBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ImageSlide);

Categories

Resources