You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.3 KiB
97 lines
2.3 KiB
package org.r3pek.pharmacies.widgets;
|
|
|
|
import org.r3pek.pharmacies.R;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Matrix;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.util.AttributeSet;
|
|
import android.view.Display;
|
|
import android.view.View;
|
|
import android.view.WindowManager;
|
|
|
|
public class Arrow extends View implements ImageOrientationSensor {
|
|
|
|
/* Orientation
|
|
* 0 -> portrait
|
|
* 1 -> landscape
|
|
* 2 -> inverted portrait
|
|
* 3 -> inverted landscape
|
|
*/
|
|
private final int orientationCompensation[] = {0, 90, 180, 270};
|
|
|
|
private Context ctx;
|
|
private float bearing;
|
|
private float orientation;
|
|
private Drawable d;
|
|
|
|
public Arrow(Context context) {
|
|
super(context);
|
|
|
|
ctx = context;
|
|
setDrawable();
|
|
}
|
|
|
|
public Arrow(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
|
|
ctx = context;
|
|
setDrawable();
|
|
}
|
|
|
|
public Arrow(Context context, AttributeSet attrs, int defStyle) {
|
|
super(context, attrs, defStyle);
|
|
|
|
ctx = context;
|
|
setDrawable();
|
|
}
|
|
|
|
private void setDrawable() {
|
|
d = ctx.getResources().getDrawable(R.drawable.arrow);
|
|
}
|
|
|
|
@Override
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
|
|
Display display = ((WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
|
|
|
float rotation = bearing - orientation - orientationCompensation[display.getOrientation()];
|
|
|
|
float keyPoints[] = {0, -9, -5, 9, 5, 9};
|
|
Matrix matrix = new Matrix();
|
|
matrix.preRotate(rotation);
|
|
matrix.mapPoints(keyPoints);
|
|
float maxX, maxY;
|
|
maxX = maxY = Float.MIN_VALUE;
|
|
float minX, minY;
|
|
minX = minY = Float.MAX_VALUE;
|
|
for (int i = 0; i < keyPoints.length; i++) {
|
|
if (i % 2 == 0) {
|
|
if (maxX < keyPoints[i]) maxX = keyPoints[i];
|
|
if (minX > keyPoints[i]) minX = keyPoints[i];
|
|
} else {
|
|
if (maxY < keyPoints[i]) maxY = keyPoints[i];
|
|
if (minY > keyPoints[i]) minY = keyPoints[i];
|
|
}
|
|
}
|
|
|
|
canvas.save();
|
|
canvas.translate((maxX + minX) / -2f, (maxY + minY) / -2f);
|
|
canvas.rotate(rotation, 10, 10);
|
|
d.setBounds(0, 0, 20, 20);
|
|
d.draw(canvas);
|
|
canvas.restore();
|
|
}
|
|
|
|
public void setBearing(float bearing) {
|
|
this.bearing = bearing;
|
|
}
|
|
|
|
public void setOrientation(float orientation) {
|
|
this.orientation = orientation;
|
|
postInvalidate();
|
|
}
|
|
|
|
} |