Simple app using drag and drop
This app demonstrates picture display and output, click on picture:
app/res/drawable - pictures
There are two images moved into here. One is called star.gif and is a red filled-in star with yellow backgroun. The other is called empty.gif and is just yellow. Both are the same size.
app/res/values/strings.xml - strings
Touch
"start x = "
" start y = "
"moving x = "
" moving y = "
"end x = "
" end y = "
"star"
app/res/layout/activity_main.xml - screen format
app/java/com.(the project name)/MainActivity - main code for app
package com.edkins.touch;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public static int [] starxy = new int [2];
public static int [] emptyxy = new int [2];
public static int w;
public static int h;
public static int pic;
public static int swap=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swap=0; pic=0;
}
public boolean onTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
if (action==MotionEvent.ACTION_DOWN) {
float x = ev.getRawX();
float y = ev.getRawY();
TextView xV = (TextView) findViewById(R.id.xsVal);
String xOut = x+"";
xV.setText(xOut);
TextView yV = (TextView) findViewById(R.id.ysVal);
String yOut =y+"";
yV.setText(yOut);
TextView xV2 = (TextView) findViewById(R.id.xmVal);
xV2.setText("");
TextView yV2 = (TextView) findViewById(R.id.ymVal);
yV2.setText("");
TextView xV3 = (TextView) findViewById(R.id.xeVal);
xV3.setText("");
TextView yV3 = (TextView) findViewById(R.id.yeVal);
yV3.setText("");
ImageView starV = (ImageView) findViewById(R.id.starPix);
starV.getLocationInWindow(starxy);
w = starV.getWidth();
h = starV.getWidth();
ImageView gridV2 = (ImageView) findViewById(R.id.emptyPix);
gridV2.getLocationInWindow(emptyxy);
pic=0;
if (x>starxy[0] && x<(starxy[0]+w) && y>starxy[1] && y<(starxy[1]+h)) {pic=1;}
if (x>emptyxy[0] && x<(emptyxy[0]+w) && y>emptyxy[1] && y<(emptyxy[1]+h)) {pic=2;}
}
else if (action==MotionEvent.ACTION_MOVE) {
float x = ev.getRawX();
float y = ev.getRawY();
TextView xV = (TextView) findViewById(R.id.xmVal);
String xOut = x+"";
xV.setText(xOut);
TextView yV = (TextView) findViewById(R.id.ymVal);
String yOut =y+"";
yV.setText(yOut);
if (pic==1 && x>emptyxy[0] && x<(emptyxy[0]+w) && y>emptyxy[1] && y<(emptyxy[1]+h)) {
swap(); pic=0;
}
if (pic==2 && x>starxy[0] && x<(starxy[0]+w) && y>starxy[1] && y<(starxy[1]+h)) {
swap(); pic=0;
}
}
else if (action==MotionEvent.ACTION_UP) {
float x = ev.getRawX();
float y = ev.getRawY();
TextView xV = (TextView) findViewById(R.id.xeVal);
String xOut = x+"";
xV.setText(xOut);
TextView yV = (TextView) findViewById(R.id.yeVal);
String yOut =y+"";
yV.setText(yOut);
}
return true;
}
public void swap() {
if (swap==0) {
ImageView star = (ImageView) findViewById(R.id.starPix);
star.setImageResource(R.drawable.empty);
ImageView empty = (ImageView) findViewById(R.id.emptyPix);
empty.setImageResource(R.drawable.star);
swap = 1;
}
else {
ImageView star = (ImageView) findViewById(R.id.starPix);
star.setImageResource(R.drawable.star);
ImageView empty = (ImageView) findViewById(R.id.emptyPix);
empty.setImageResource(R.drawable.empty);
swap = 0;
}
}
}
Index to my other websites
© Jo Edkins 2016