gpt4 book ai didi

java - 捏合缩放和平移

转载 作者:行者123 更新时间:2023-12-01 13:47:45 27 4
gpt4 key购买 nike

我有一个以 LinearLayout 作为其主要布局的 Activity 。在该布局中,有一个按钮可将 View (R.layout.motor_block) 添加到主布局 (R.id.layout):

    LayoutInflater inflater =
(LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View iv = inflater.inflate( R.layout.motor_block, null );
RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
rl.addView(iv);

这工作正常,但是当我添加更多 View 时,屏幕会被它们填满,因此我需要一种通过添加平移来“扩展”大小的方法,以便我可以浏览所有 View 。我还想添加缩放功能。

我尝试使用 WebView:

       RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setSupportZoom(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setDisplayZoomControls(false);
wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
wv.addView(iv);

View 被添加到其中,并且平移可以工作。问题是,当我缩放时,webView 会缩放背景,而 View 不会改变大小。

我还尝试了一些自定义 View ,但它们不支持带有 subview 的 View 。

实现这一目标的最佳方法是什么?

最佳答案

只是一个例子。在主容器 View 上使用 setTranslationX/Y 和 setScaleX/Y。它的子级将随之缩放和翻译。

public class MainActivity extends Activity {

private RelativeLayout mainLayout;
private ScaleGestureDetector scaleGestureDetector;
private float scale = 1;
private PointF touchPoint;
private PointF pan;

private boolean isScaling;
private boolean endScalingNextUp;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (RelativeLayout) findViewById(id.main_layout);
scaleGestureDetector = new ScaleGestureDetector(this, new ExampleScaleGestureListener());
Button b = ((Button) findViewById(id.btn_reset));
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
reset();
}
});
reset();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);

if (scale != 1 && !isScaling) {
float x = event.getRawX();
float y = event.getRawY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touchPoint = new PointF(x, y);
} else if (event.getAction() == MotionEvent.ACTION_MOVE && touchPoint != null) {
pan.x = x - touchPoint.x;
pan.y = y - touchPoint.y;
panView();
}
}
if (isScaling && endScalingNextUp) {
if (event.getAction() == MotionEvent.ACTION_POINTER_UP) {
endScalingNextUp = false;
isScaling = false;
}
}

return true;
}

private void setPivot(float focusX, float focusY) {
mainLayout.setPivotX(focusX);
mainLayout.setPivotY(focusY);
}

private void scaleView() {
mainLayout.setScaleX(scale);
mainLayout.setScaleY(scale);
}

private void panView() {
mainLayout.setTranslationX(pan.x);
mainLayout.setTranslationY(pan.y);
}

private void reset() {
setPivot(0, 0);
scale = 1;
scaleView();
pan = new PointF(0, 0);
panView();
isScaling = false;
}

private class ExampleScaleGestureListener implements OnScaleGestureListener {

private static final float SCALE_SPEED = .02f;

@Override
public void onScaleEnd(ScaleGestureDetector detector) {
endScalingNextUp = true;
}

@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
float focusX = detector.getFocusX();
float focusY = detector.getFocusY();
setPivot(focusX, focusY);
isScaling = true;
endScalingNextUp = false;
return true;
}

@Override
public boolean onScale(ScaleGestureDetector detector) {
isScaling = true;
endScalingNextUp = false;
if (detector.getScaleFactor() < 1) {
scale -= SCALE_SPEED;
} else if (detector.getScaleFactor() > 1) {
scale += SCALE_SPEED;
}
scaleView();
return true;
}
}
}


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/main_layout"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tv"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv"
android:layout_marginBottom="69dp"
android:layout_marginRight="29dp"
android:layout_toLeftOf="@+id/tv"
android:src="@drawable/ic_launcher" />

<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Reset" />

</RelativeLayout>

关于java - 捏合缩放和平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20225265/

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