Implement click detection.
This commit is contained in:
parent
bc33baca27
commit
078b06ab57
@ -5,19 +5,19 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
|
||||||
import com.codigoparallevar.minicards.parts.Part;
|
import com.codigoparallevar.minicards.parts.Part;
|
||||||
import com.codigoparallevar.minicards.parts.Placeholder;
|
import com.codigoparallevar.minicards.parts.Placeholder;
|
||||||
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
class DrawView extends View {
|
class DrawView extends View {
|
||||||
|
|
||||||
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
||||||
List<Part> parts = new LinkedList<>();
|
ArrayList<Part> parts = new ArrayList<>();
|
||||||
|
|
||||||
public DrawView(Context context) {
|
public DrawView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@ -34,4 +34,32 @@ class DrawView extends View {
|
|||||||
|
|
||||||
Log.d("Render time", System.currentTimeMillis() - time + "ms");
|
Log.d("Render time", System.currentTimeMillis() - time + "ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouchEvent(MotionEvent event){
|
||||||
|
int x = (int) event.getX() - this.getLeft();
|
||||||
|
int y = (int) event.getY() - this.getTop();
|
||||||
|
|
||||||
|
Part touchedPart = getPartOn(x, y);
|
||||||
|
if (touchedPart == null){
|
||||||
|
Log.d("Touched part", "not found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("Touched part", "Part: " + touchedPart);
|
||||||
|
invalidate();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Part getPartOn(int x, int y) {
|
||||||
|
// Look in the list, in reverse so top-most elements are checked before
|
||||||
|
for (int i = parts.size() - 1; i >= 0; i--){
|
||||||
|
Part part = parts.get(i);
|
||||||
|
if ((x >= part.getLeft()) && (part.getRight() >= x)
|
||||||
|
&& (y >= part.getTop()) && (part.getBottom() >= y)){
|
||||||
|
return part;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,23 @@ package com.codigoparallevar.minicards;
|
|||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
|
||||||
public class TestActivity extends AppCompatActivity {
|
public class TestActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
DrawView drawView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
DrawView drawView = new DrawView(this);
|
drawView = new DrawView(this);
|
||||||
drawView.setBackgroundColor(Color.BLACK);
|
drawView.setBackgroundColor(Color.BLACK);
|
||||||
setContentView(drawView);
|
setContentView(drawView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
|
return drawView.onTouchEvent(event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user