public class Customview extends View
implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener,
ScaleGestureDetector.OnScaleGestureListener {
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_UP
onSingleTapUp
onSingleTapConfirmed
onTouchEvent⇒ACTION_DOWN
onDown
onShowPress
onTouchEvent⇒ACTION_UP
onSingleTapUp
onSingleTapConfirmed
onTouchEvent⇒ACTION_DOWN
onDown
onShowPress
onLongPress
onTouchEvent⇒ACTION_UP
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_UP
onSingleTapUp
onTouchEvent⇒ACTION_DOWN
onDoubleTap
onDoubleTapEvent
onDown
onTouchEvent⇒ACTION_UP
onDoubleTapEvent
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_MOVE
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
:
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
onScaleBegin ⇒ピンチ操作の開始(指2本でタッチした時)
onScale ⇒ピンチ操作中に繰り返し呼ばれる
onScale
onScale
:
:
:
onScale
onScaleEnd ⇒ピンチ操作の終了(どちらかの指が離れて、1本になった時)
onTouchEvent⇒ACTION_UP
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_MOVE
onTouchEvent⇒ACTION_MOVE
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
:
:
:
onTouchEvent⇒ACTION_UP
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_UP
onSingleTapUp
onTouchEvent⇒ACTION_DOWN
onDoubleTap
onDoubleTapEvent
onDown
onShowPress
onTouchEvent⇒ACTION_MOVE
onDoubleTapEvent
onTouchEvent⇒ACTION_MOVE
onDoubleTapEvent
onTouchEvent⇒ACTION_MOVE
onDoubleTapEvent
:
onTouchEvent⇒ACTION_MOVE
onDoubleTapEvent
onScaleBegin
onScale
onScale
onScale
:
:
:
onScale
onScaleEnd
onTouchEvent⇒ACTION_DOWN
onDown
onTouchEvent⇒ACTION_MOVE
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_MOVE
onScroll
:
onTouchEvent⇒ACTION_MOVE
onScroll
onTouchEvent⇒ACTION_UP
onFling!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/baloon" ⇒吹き出しの9-patchイメージ
android:orientation="horizontal" >
<ImageView
android:id="@+id/popupicon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:src="@drawable/popupicon" /> ⇒アイコンイメージ
<TextView
android:id="@+id/popuptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="notitle"
android:textColor="#ffffff"
android:textSize="20sp" />
</LinearLayout>
@Override
public void onLongPress(MotionEvent e) {
LinearLayout layout = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.popupwindow, null);
// Textを設定・TextViewの取得は、layoutから指定する必要がある
TextView poptext = (TextView)layout.findViewById(R.id.popuptext);
poptext.setText(String.format("%d, %d",(int)e.getX(),(int)e.getY()));
// PopupWindowはActivityから呼出されるので、予め取得しておいたメインのActivityを指定
PopupWindow popupWindow = new PopupWindow(activity);
popupWindow.setContentView(layout);
// PopupWindowの背景に、透明のイメージを指定
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparent));
// PopupWindowの外側をタッチすると、消えるようにする
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// showAsDropDownで表示する時は、setWindowLayoutModeで大きさを指定する必要がある
popupWindow.setWindowLayoutMode(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAsDropDown(this,(int)(e.getX()),(int)(e.getY()-view_h));
}
if (popupWindow!=null && popupWindow.isShowing()) {
popupWindow.dismiss();
}
View v = popupWindow.getContentView();
w = v.getWidth();
h = v.getHeight();
View v = popupWindow.getContentView();
v.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int w = v.getMeasuredWidth();
int h = v.getMeasuredHeight();
LinearLayout layout = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.popupwindow, null);
:
// Closeボタン
ImageButton popupClose = (ImageButton)layout.findViewById(R.id.closebtn);
popupClose.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
popupWindow.dismiss();
}
});
android:background="@drawable/close"
// showAtLocationは、setWidth,setHeightで、大きさを指定しないとダメらしい
popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(this,Gravity.TOP|Gravity.LEFT,(int)e.getX(),(int)e.getY());
Parameter | Decimal | Hex | 意味 |
---|---|---|---|
TOP | 48 | 0x30 | コンテナ上部に配置 |
BOTTOM | 80 | 0x50 | コンテナ下部に配置 |
LEFT | 3 | 0x03 | コンテナ左側に配置 |
RIGHT | 5 | 0x05 | コンテナ右側に配置 |
CENTER | 17 | 0x11 | コンテナ中央に配置 |
CENTER_HORIZONTAL | 1 | 0x01 | コンテナ左右の中央に配置 |
CENTER_VERTICAL | 16 | 0x10 | コンテナ上下の中央に配置 |
NO_GRAVITY | 0 | 0x00 | 指定なし |
FILL | 119 | 0x77 | |
FILL_HORIZONTAL | 7 | 0x07 | |
FILL_VERTICAL | 112 | 0x70 | |
CLIP_HORIZONTAL | 8 | 0x08 | |
CLIP_VERTICAL | 128 | 0x80 | |
START | 8388611 | 0x800003 | |
END | 8388613 | 0x800005 | |
AXIS_CLIP | 8 | 0x08 | |
AXIS_PULL_AFTER | 4 | 0x04 | |
AXIS_PULL_BEFORE | 2 | 0x02 | |
AXIS_SPECIFIED | 1 | 0x01 | |
AXIS_X_SHIFT | 0 | 0x00 | |
AXIS_Y_SHIFT | 4 | 0x04 | |
DISPLAY_CLIP_HORIZONTAL | 16777216 | 0x1000000 | |
DISPLAY_CLIP_VERTICAL | 268435456 | 0x10000000 | |
HORIZONTAL_GRAVITY_MASK | 7 | 0x07 | |
VERTICAL_GRAVITY_MASK | 112 | 0x70 | |
RELATIVE_HORIZONTAL_GRAVITY_MASK | 8388615 | 0x800007 | |
RELATIVE_LAYOUT_DIRECTION | 8388608 | 0x800000 |
cv = (customView)findViewById(R.id.myView1);
registerForContextMenu(cv);
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
int viewId = view.getId();
if (viewId == R.id.myView1) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_menu1:
showMsg("item0 selected");
return true;
case R.id.context_menu2:
showMsg("item1 selected");
return true;
default:
return super.onContextItemSelected(item);
}
}
myActivity.openContextMenu(this);