Android ++
This commit is contained in:
@@ -7,8 +7,8 @@ android {
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.reticule"
|
||||
minSdk 19
|
||||
targetSdk 32
|
||||
minSdk 21
|
||||
targetSdk 33
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
|
||||
|
@@ -11,4 +11,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
ReticuleView rv = (ReticuleView) findViewById(R.id.mimemamomou);
|
||||
rv.setOnTouchListener(new ReticuleMovementListener());
|
||||
}
|
||||
}
|
@@ -1,2 +1,37 @@
|
||||
package com.example.reticule;public class ReticuleMovementListener {
|
||||
package com.example.reticule;
|
||||
|
||||
import android.view.DragEvent;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
public class ReticuleMovementListener implements View.OnTouchListener {
|
||||
|
||||
private MotionEvent.PointerCoords pc = new MotionEvent.PointerCoords();
|
||||
private float cx, cy;
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View view, MotionEvent motionEvent) {
|
||||
ReticuleView v = (ReticuleView) view;
|
||||
|
||||
switch (motionEvent.getActionMasked()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
motionEvent.getPointerCoords(motionEvent.getActionIndex(), pc);
|
||||
cx = v.getCx();
|
||||
cy = v.getCy();
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
MotionEvent.PointerCoords npc = new MotionEvent.PointerCoords();
|
||||
motionEvent.getPointerCoords(motionEvent.getActionIndex(), npc);
|
||||
|
||||
float dx = npc.x - pc.x;
|
||||
float dy = npc.y - pc.y;
|
||||
v.setCx(cx + dx);
|
||||
v.setCy(cy + dy);
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -1,2 +1,62 @@
|
||||
package com.example.reticule;public class ReticuleView {
|
||||
package com.example.reticule;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ReticuleView extends View {
|
||||
|
||||
private float cx, cy;
|
||||
private final int radius = 15;
|
||||
private Paint backgroundPaint, crosshairPaint;
|
||||
|
||||
public ReticuleView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
cx = getWidth() / 2;
|
||||
cy = getHeight() / 2;
|
||||
|
||||
crosshairPaint = new Paint();
|
||||
crosshairPaint.setStyle(Paint.Style.STROKE);
|
||||
crosshairPaint.setColor(0xFFFF9900);
|
||||
|
||||
backgroundPaint = new Paint();
|
||||
backgroundPaint.setStyle(Paint.Style.FILL);
|
||||
backgroundPaint.setColor(0xFF888888);
|
||||
}
|
||||
|
||||
private double distance(float x, float y) {
|
||||
return Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y - cy, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
|
||||
canvas.drawCircle(cx, cy, 15, crosshairPaint);
|
||||
canvas.drawLine(cx - 20, cy, cx + 20, cy, crosshairPaint);
|
||||
canvas.drawLine(cx, cy - 20, cx, cy + 20, crosshairPaint);
|
||||
}
|
||||
|
||||
public void setCx(float cx) {
|
||||
this.cx = cx;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void setCy(float cy) {
|
||||
this.cy = cy;
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public float getCx() {
|
||||
return cx;
|
||||
}
|
||||
|
||||
public float getCy() {
|
||||
return cy;
|
||||
}
|
||||
}
|
||||
|
@@ -6,13 +6,9 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
<com.example.reticule.ReticuleView
|
||||
android:id="@+id/mimemamomou"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Reference in New Issue
Block a user