最後の編集をご覧ください
----------------------------------------------
GestureDetector.OnGestureListener インターフェイスを実装し、onDown、onScroll メソッドを使用してジェスチャを検出し、ビューの onDraw(Canvas c) メソッドを使用して、位置を変更した後にアナログのビットマップを再描画 (および変換) する必要があります。必要に応じてマトリックスで)
この回答のように onTouchEvent を使用することもできます:
GestureDetector の OnUp イベント
---------------------------------------------------------------------------------------
編集
ここでは、ビットマップを使用せずに drawCircle メソッドを試してみます。
複雑すぎません。高速に記述したため、改善点は確かにありますが、
そのままで非常にうまく機能します。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class AnalogueView extends View {
float x,y;
double r,t;
int cx, cy,w,h;
final int RADIUS = 20;
Paint black = new Paint();
Paint grey = new Paint();
Paint white = new Paint();
private int toDo;
public AnalogueView(Context context, AttributeSet attrs) {
super(context, attrs);
black.setColor(Color.BLACK);
grey.setColor(Color.GRAY);
white.setColor(Color.WHITE);
black.setFlags(Paint.ANTI_ALIAS_FLAG);
white.setFlags(Paint.ANTI_ALIAS_FLAG);
grey.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
cx = w/2;
cy = h/2;
x = cx;y=cy;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawMyStuff(canvas);
switch (toDo) {
case 1:
center();
break;
default:
break;
}
}
private void drawMyStuff(final Canvas canvas) {
canvas.drawCircle(cx, cy, w/2, black);
canvas.drawCircle(cx, cy, w/2-5, grey);
canvas.drawCircle(cx, cy, w/2-10, black);
canvas.drawCircle(x, y, RADIUS+2, white);
canvas.drawCircle(x, y, RADIUS, grey);
}
// n2p : normal to polar coordinates conversion
// p2n : polar to normal coordinates conversion
// R : distance to polar center
// T : polar angle
double n2pR(double x, double y){
return distance(x,y,cx,cy);
}
double n2pT(double x, double y){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(double r, double t){
return r*Math.cos(t) + cx;
}
double p2nY(double r, double t){
return r*Math.sin(t) + cy;
}
double n2pR(){
return distance(x,y,cx,cy);
}
double n2pT(){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(){
return r*Math.cos(t) + cx;
}
double p2nY(){
return r*Math.sin(t) + cy;
}
double distance(double x1 , double y1, double x2, double y2 ){
return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
updatePosition(event);
break;
case MotionEvent.ACTION_MOVE :
updatePosition(event);
break;
case MotionEvent.ACTION_UP :
toDo = 1;
center();
break;
default :break;
}
return true;
}
private void center(){
if(r>15){
r-=15;
}else{
toDo=0;
r=0;
}
x = (float) p2nX();
y= (float) p2nY();
invalidate();
}
void updatePosition(MotionEvent e){
r= Math.min(w/2-RADIUS, n2pR(e.getX(),e.getY()));
t = n2pT(e.getX(),e.getY());
x = (float) p2nX();
y =(float) p2nY();
invalidate();
}
}
レイアウトに追加する方法は次のとおりです。
<yourpackage.AnalogueView
android:id="@+id/analogueView1"
android:layout_width="80px"
android:layout_height="80px"
/>
また、さまざまなアナログ イベントを検出するために、必要なメソッド定義を含むリスナーを追加し、AnalogView 内でインターフェイスを使用できます。次に、AnalogView の動作を実装する場合は、setter を呼び出してインターフェイスに実装を追加するだけです。リスナー用 (ボタンの setOnClickListener のようなもの)。このようにして、他のアプリで再利用できる完全にカプセル化された AnalogView が作成されます。
-------------------------------------------------------------------------------
最終編集:リスナーによるイベントの処理:
AnalogueView に Listener を埋め込みました。これで、ボタンに OnClickListener を使用するのと同じ方法で使用できます。
setOnMoveListener メソッドを使用して、2 つのメソッドの実装を提供します。
onMaxMoveInDirection(double polar_angle_of_direction) and
onHalfMoveInDirection(double polar_angle_of_direction)
以下は、AnalogueView の新しいコードです (大きな変更はなく、インターフェース定義、moveListener フィールド、および updatePosition でアクションを起動するだけです)。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class AnalogueView extends View {
OnMoveListener moveListener;
float x,y;
double r,t;
int cx, cy,w,h;
final int RADIUS = 20;
Paint black = new Paint();
Paint grey = new Paint();
Paint white = new Paint();
private int toDo;
public AnalogueView(Context context, AttributeSet attrs) {
super(context, attrs);
black.setColor(Color.BLACK);
grey.setColor(Color.GRAY);
white.setColor(Color.WHITE);
black.setFlags(Paint.ANTI_ALIAS_FLAG);
white.setFlags(Paint.ANTI_ALIAS_FLAG);
grey.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
cx = w/2;
cy = h/2;
x = cx;y=cy;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawMyStuff(canvas);
switch (toDo) {
case 1:
center();
break;
default:
break;
}
}
private void drawMyStuff(final Canvas canvas) {
canvas.drawCircle(cx, cy, w/2, black);
canvas.drawCircle(cx, cy, w/2-5, grey);
canvas.drawCircle(cx, cy, w/2-10, black);
canvas.drawCircle(x, y, RADIUS+2, white);
canvas.drawCircle(x, y, RADIUS, grey);
}
// n2p : normal to polar coordinates conversion
// p2n : polar to normal coordinates conversion
// R : distance to polar center
// T : polar angle
double n2pR(double x, double y){
return distance(x,y,cx,cy);
}
double n2pT(double x, double y){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(double r, double t){
return r*Math.cos(t) + cx;
}
double p2nY(double r, double t){
return r*Math.sin(t) + cy;
}
double n2pR(){
return distance(x,y,cx,cy);
}
double n2pT(){
return Math.atan2((y-cy),(x-cx));
}
double p2nX(){
return r*Math.cos(t) + cx;
}
double p2nY(){
return r*Math.sin(t) + cy;
}
double distance(double x1 , double y1, double x2, double y2 ){
return Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN :
updatePosition(event);
break;
case MotionEvent.ACTION_MOVE :
updatePosition(event);
break;
case MotionEvent.ACTION_UP :
toDo = 1;
center();
break;
default :break;
}
return true;
}
private void center(){
if(r>15){
r-=15;
}else{
toDo=0;
r=0;
}
x = (float) p2nX();
y= (float) p2nY();
invalidate();
}
void updatePosition(MotionEvent e){
r= Math.min(w/2-RADIUS, n2pR(e.getX(),e.getY()));
t = n2pT(e.getX(),e.getY());
x = (float) p2nX();
y =(float) p2nY();
if(moveListener != null)
if(r == w/2-RADIUS)
moveListener.onMaxMoveInDirection(t);
else if(r >= w/4-RADIUS/2)
moveListener.onHalfMoveInDirection(t);
invalidate();
}
public void setOnMoveListener(OnMoveListener listener){
moveListener =listener;
}
public interface OnMoveListener{
public void onHalfMoveInDirection(double polarAngle);
public void onMaxMoveInDirection(double polarAngle);
}
}
そして、実装を提供する方法は次のとおりです。
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import com.analog.AnalogueView.OnMoveListener;
public class MainActivity extends Activity {
AnalogueView analogue;
TextView showMoveEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showMoveEvent = (TextView) findViewById(R.id.showMoveEvent);
analogue = (AnalogueView) findViewById(R.id.analogueView1);
analogue.setOnMoveListener(new OnMoveListener() {
@Override
public void onMaxMoveInDirection(double polarAngle) {
showMoveEvent.setText("max move in "+polarAngle+" direction");
}
@Override
public void onHalfMoveInDirection(double polarAngle) {
showMoveEvent.setText("half move in "+polarAngle+" direction");
}
});
}
}
結果: テキスト フィールドにイベントを表示:
