我正在尝试制作可以缩放和滚动的自定义相对布局。现在我试图实现规模化。现在我已将自定义相对布局作为另一个相对布局的父布局,其中包含可触摸的 Imageview 作为其子布局。现在,当我缩放父级自定义相对布局时,子级也得到缩放,但 Imageview 的可点击区域翻译我不知道为什么?当 Imageview 或布局处于正常位置时,可点击区域位于 Imageview 上,但是一旦布局缩放,可点击区域就会移动?我不知道为什么我面临可点击的奇怪位置位移
这是代码
我的自定义相对布局
public class scaleLayout extends RelativeLayout {
private float mScaleFactor=1.0f;
private long lastTouchTime = -1;
public scaleLayout(Context context)
{
super(context);
// setWillNotDraw(false);
}
public scaleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//setWillNotDraw(false);
// TODO Auto-generated constructor stub
}
/* @Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
} */
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
long thisTime = System.currentTimeMillis();
if (thisTime - lastTouchTime < 250) {
// Double tap
mScaleFactor=1.5f;
invalidate();
lastTouchTime = -1;
} else {
// Too slow :)
/* mScaleFactor=1.0f;
invalidate();*/
lastTouchTime = thisTime;
}
}
return super.onInterceptTouchEvent(ev);
}
@Override
protected void dispatchDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.save(Canvas.MATRIX_SAVE_FLAG);
canvas.scale(mScaleFactor, mScaleFactor);
super.dispatchDraw(canvas);
canvas.restore();
}
@Override
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
// TODO Auto-generated method stub
return super.invalidateChildInParent(location, dirty);
}
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
int count = getChildCount();
for(int i=0;i<count;i++){
View child = getChildAt(i);
if(child.getVisibility()!=GONE){
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
child.layout(
(int)(params.leftMargin * mScaleFactor),
(int)(params.topMargin * mScaleFactor),
(int)((params.leftMargin + child.getMeasuredWidth()) * mScaleFactor),
(int)((params.topMargin + child.getMeasuredHeight()) * mScaleFactor)
);
}
}
}
这是活动
public class LayoutZoomingActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
img1.setOnTouchListener(this);
ImageView img2 = (ImageView) findViewById(R.id.imageView2);
img2.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ImageView iv= (ImageView) v;
v.setVisibility(View.INVISIBLE);
return false;
}
这是xml
<com.layoutzooming.scaleLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gm01"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="jhkibnkij"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:textSize="25dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="500dp"
android:layout_marginTop="250dp"
android:background="#000"
android:src="@drawable/dih01" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="350dp"
android:layout_marginTop="250dp"
android:background="#000"
android:src="@drawable/dih02" />
</RelativeLayout>
</com.layoutzooming.scaleLayout>