私はstarmapのシステムを表す25個のイメージビューを追加するパンニングズーム相対レイアウトを持っています。これは画面の2/3を占めます。もう1つの1/3は、対応するシステムイメージビューがクリックされたときの情報を表示します。しかし、私のonClickListenerメソッドはトリガされていません。私はonClickメソッドの開始点の近くにブレークポイントを持つデバッグモードを使用して、それがトリガされていないことを確認し、正しく表示しませんでした。ここに関連コードがあります。参照、インタラクティブなビュークラスのもカスタムImageViewのonClickListenerが登録されていないのはなぜですか?
public class StarMapActivity extends AppCompatActivity implements View.OnClickListener {
InteractiveView starMap;
TextView systemNameView;
TextView systemFactionView;
TextView systemCoordinatesView;
TextView systemPlanetsNumberView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_star_map);
starMap = (InteractiveView) findViewById(R.id.myStarMapInteractiveView);
systemNameView = (TextView) findViewById(R.id.systemNameView);
systemFactionView = (TextView) findViewById(R.id.systemFactionView);
systemCoordinatesView = (TextView) findViewById(R.id.systemCoordinatesView);
systemPlanetsNumberView = (TextView) findViewById(R.id.systemPlanetsNumberView);
for (int i = 0; i < 25; i++) { plotSystem(PlayerCharacterInfo.currentPlayerCharacter.occupiedSector.starSystems[i], i);
}
}
void plotSystem(StarSystem systemToDraw, int systemArrayId){ //draws the white circle images that represent the systems on the map
StarSystemView viewToDraw = new StarSystemView(this, getResources().getString(systemToDraw.nameId), systemArrayId);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ShapeDrawable myShape = new ShapeDrawable(new OvalShape());
myShape.setIntrinsicHeight(10);
myShape.setIntrinsicWidth(10);
myShape.getPaint().setColor(-1);
layoutParams.leftMargin = (systemToDraw.coordinates.x - 50) * 4;
layoutParams.topMargin = (systemToDraw.coordinates.y - 50) * 4;
viewToDraw.setImageDrawable(myShape);
viewToDraw.setOnClickListener(this);
viewToDraw.setFocusable(true);
viewToDraw.setClickable(true);
starMap.addView(viewToDraw, layoutParams);
viewToDraw.bringToFront();
}
public void onClick(View view){
StarSystemView clickedView = (StarSystemView) view;
StarSystem systemToView = PlayerCharacterInfo.currentPlayerCharacter.occupiedSector.starSystems[clickedView.systemArrayId];
systemNameView.setText(getResources().getString(systemToView.nameId));
switch(systemToView.systemFactionId){
case 0:
systemFactionView.setText(getResources().getString(R.string.faction_neutral));
break;
case 1:
systemFactionView.setText(getResources().getString(R.string.faction_imperial));
break;
case 2:
systemFactionView.setText(getResources().getString(R.string.faction_federation));
break;
case 3:
systemFactionView.setText(getResources().getString(R.string.faction_republic));
break;
}
systemCoordinatesView.setText(Integer.toString(systemToView.coordinates.x)+", "+Integer.toString(systemToView.coordinates.y));
switch(systemToView.numberOfPlanets){
case 1:
systemPlanetsNumberView.setText(getResources().getString(R.string.basic_one));
break;
case 2:
systemPlanetsNumberView.setText(getResources().getString(R.string.basic_two));
break;
case 3:
systemPlanetsNumberView.setText(getResources().getString(R.string.basic_three));
break;
}
}
:参照用にも
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.content.Context;
import android.graphics.Canvas;
import java.lang.Math;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class InteractiveView extends RelativeLayout {
private float mPositionX = 0;
private float mPositionY = 0;
float mScale = 1.0f;
public InteractiveView(Context context) {
super(context);
this.setWillNotDraw(false);
this.setOnTouchListener(mTouchListener);
}
public InteractiveView(Context context, AttributeSet attrs){
super(context, attrs);
this.setWillNotDraw(false);
this.setOnTouchListener(mTouchListener);
}
public void setMovingPosition(float lPositionX, float lPositionY){
mPositionX += lPositionX;
mPositionY += lPositionY;
}
@Override
protected void dispatchDraw(Canvas canvas) {
if (mScale < 0.5){
mScale = 0.5f;
} else if (mScale > 5){
mScale = 5f;
}
canvas.save();
canvas.translate(getWidth()/2, getHeight()/2);
canvas.translate(mPositionX*mScale, mPositionY*mScale);
canvas.scale(mScale, mScale);
super.dispatchDraw(canvas);
canvas.restore();
}
// touch events
private final int NONE = 0;
private final int DRAG = 1;
private final int ZOOM = 2;
private final int CLICK = 3;
// pinch to zoom
private float mOldDist;
private float mNewDist;
private float mScaleFactor = 0.01f;
// position
private float mPreviousX;
private float mPreviousY;
int mode = NONE;
public OnTouchListener mTouchListener = new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN: // one touch: drag
mode = CLICK;
break;
case MotionEvent.ACTION_POINTER_2_DOWN: // two touches: zoom
mOldDist = spacing(e);
mode = ZOOM; // zoom
break;
case MotionEvent.ACTION_UP: // no mode
mode = NONE;
break;
case MotionEvent.ACTION_POINTER_2_UP: // no mode
mode = NONE;
break;
case MotionEvent.ACTION_MOVE: // rotation
if (e.getPointerCount() > 1 && mode == ZOOM) {
mNewDist = spacing(e) - mOldDist;
mScale += mNewDist*mScaleFactor;
invalidate();
mOldDist = spacing(e);
} else if (mode == CLICK || mode == DRAG) {
float dx = (x - mPreviousX)/mScale;
float dy = (y - mPreviousY)/mScale;
setMovingPosition(dx, dy);
invalidate();
mode = DRAG;
}
break;
}
mPreviousX = x;
mPreviousY = y;
return true;
}
};
// finds spacing
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
}
ImageViewのクラス
public class StarSystemView extends ImageView {
String systemName;
int systemArrayId;
public StarSystemView(Context context, String systemNameToSet, int arrayIdToSet){
super(context);
systemName = systemNameToSet;
systemArrayId = arrayIdToSet;
}
}
私は私がしましたsetOnClickListener(StarMapActivity.this) を使用して試してみましたコンテンツをラップするのではなく、StarSystemViewsのレイアウトパラメータを12と12に設定してみました。 インタラクティブなビューをフォーカス可能にしました。どこかでそれを読んでいます。 Android Developerのウェブサイトをダブルチェックして、タイプミスやコーディングエラーが発生しているかどうかわかりません。
ズーム可能なレイアウト内のimageviewsが単純に解除不可能なのか疑問に思っています。
ご協力いただきありがとうございます。
ズーム可能なimageViewのため、onInterceptTouchEventメソッドを使用する必要があります。 –
画像ビュー自体はズームできません。これはズーム可能なレイアウトの子です。それが違いを生むかどうかはわかりません。 – Shieldwall