mini-cards/app/src/main/java/com/codigoparallevar/minicards/DrawView.java

66 lines
1.8 KiB
Java
Raw Normal View History

2017-07-03 18:43:22 +00:00
package com.codigoparallevar.minicards;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
2017-07-03 21:25:25 +00:00
import android.util.Log;
2017-07-03 21:55:32 +00:00
import android.view.MotionEvent;
2017-07-03 18:43:22 +00:00
import android.view.View;
2017-07-03 21:25:25 +00:00
import com.codigoparallevar.minicards.parts.Part;
import com.codigoparallevar.minicards.parts.Placeholder;
import com.codigoparallevar.minicards.parts.buttons.RoundButton;
2017-07-03 21:55:32 +00:00
import java.util.ArrayList;
2017-07-03 21:25:25 +00:00
2017-07-03 18:43:22 +00:00
class DrawView extends View {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
2017-07-03 21:55:32 +00:00
ArrayList<Part> parts = new ArrayList<>();
2017-07-03 18:43:22 +00:00
public DrawView(Context context) {
super(context);
paint.setColor(Color.RED);
2017-07-03 21:25:25 +00:00
parts.add(new RoundButton(500, 1200, 80, 100));
2017-07-03 18:43:22 +00:00
}
@Override
public void onDraw(Canvas canvas){
2017-07-03 21:25:25 +00:00
long time = System.currentTimeMillis();
for (Part part : parts){
part.draw(canvas);
}
Log.d("Render time", System.currentTimeMillis() - time + "ms");
2017-07-03 18:43:22 +00:00
}
2017-07-03 21:55:32 +00:00
@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;
}
2017-07-03 18:43:22 +00:00
}