gpt4 book ai didi

android - 自定义相对布局缩放?

转载 作者:太空狗 更新时间:2023-10-29 12:53:13 24 4
gpt4 key购买 nike

我正在尝试制作可以缩放和滚动的 Custom RelativeLayout。现在我试图达到规模。现在我已经将自定义相对布局作为另一个相对布局的父布局,其中包含可触摸的 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)
);
}
}
}

这是 Activity

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>

最佳答案

通过在 LayoutZoomingActivity 中添加以下代码,我设法解决了这个问题。

 public class LayoutZoomingActivity extends Activity implements OnTouchListener {

ImageView img1; ImageView img2; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1 = (ImageView) findViewById(R.id.imageView1);
img1.setOnTouchListener(this);
img2 = (ImageView) findViewById(R.id.imageView2);
img2.setOnTouchListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("b","b");
if(isViewContains(img1, (int)event.getX(), (int)event.getY())) {
img1.setVisibility(View.GONE); }
if(isViewContains(img2, (int)event.getX(), (int)event.getY())) {
img2.setVisibility(View.GONE); }

return false;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
Log.d("onTouchEvent c","c"+(int)event.getX()+","+(int)event.getY());
if(isViewContains(img1, (int)event.getX(), (int)event.getY())) {
img1.setVisibility(View.GONE);
img1.invalidate(); }
if(isViewContains(img2, (int)(event.getX()/scaleLayout.mScaleFactor), (int)(event.getY()/scaleLayout.mScaleFactor))) {
img2.setVisibility(View.GONE);
img2.invalidate(); }
return super.onTouchEvent(event);
}
private boolean isViewContains(View view, int rx, int ry) {
int[] l = new int[2];
view.getLocationOnScreen(l);
int x = l[0];
int y = l[1];
int w = view.getWidth();
int h = view.getHeight();
Log.d("isViewContains::"+x+"::"+y+"::"+w+"::"+h,rx+"::"+ry);
if (rx < x || rx > x + w || ry < y || ry > y + h) {
return false;
}
return true;
} }

你能试试这个并根据你的要求定制吗?

关于android - 自定义相对布局缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9765804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com