基于触摸缩放RelativeLayout [英] Zoom RelativeLayout Based on Touch

查看:145
本文介绍了基于触摸缩放RelativeLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用 setPivotX(float),setPivotY(float),setScaleX(float),setScaleY(float)缩放整个相对布局。但是, setPivotX(float) setPivotY(float)的行为仍然令我感到困惑。实际上我想在用户触摸的位置缩放布局。因此,在缩放布局之后,用户触摸点将位于屏幕的中间。所以,我想 setPivotX(float) setPivotY(float)应该设置为touchPoint,但这不起作用。无论如何这里是我的代码:



main.xml



< RelativeLayout 
xmlns:android =http://schemas.android.com/apk/res/android
android:orientation =horizo​​ntal
android:id =@ + id / layout
android:layout_width =fill_parent
android:layout_height =fill_parent
android:background =#ffffff>
< ImageView
android:id =@ + id / image
android:layout_width =wrap_content
android:layout_height =wrap_content
android: src =@ drawable / ic_launcher
android:visibility =invisible/>
< / RelativeLayout>





MainActivity.java



公共类MainActivity扩展Activity 
{
RelativeLayout布局;
LayoutParams params;
ImageView图片;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
image =(ImageView)findViewById(R.id.image);
layout =(RelativeLayout)findViewById(R.id.layout);
layout.setOnTouchListener(new TouchHandler());
}
私有类TouchHandler实现OnTouchListener
{
@Override
公共布尔onTouch(视图,MotionEvent事件)
{
if( event.getAction()== MotionEvent.ACTION_DOWN)
{
params.leftMargin = Math.round(event.getX()* 160f / getBaseContext()。getResources()。getDisplayMetrics()。densityDpi) ;
params.topMargin = Math.round(event.getY()* 160f / getBaseContext()。getResources()。getDisplayMetrics()。densityDpi);
image.setLayoutParams(params);
image.setVisibility(View.VISIBLE);
layout.setPivotX(event.getX());
layout.setPivotY(event.getY());
layout.setScaleX(2f);
layout.setScaleY(2f);
}
返回true;
}
}
}





注意:



我使用 ic_launcher 来识别用户接触点。因此,如果代码有效,ic_launcher将始终位于屏幕中间。

解决方案

请参阅:

缩放视图 [ ^ ]

http://developer.sonymobile.com/2010/05/18/android-one-finger-zoom-tutorial-part-1/ [<一个href =http://developer.sonymobile.com/2010/05/18/android-one-finger-zoom-tutorial-part-1/target =_ blanktitle =New Window> ^ ]

另外:

捏合以缩放Android中的多点触控功能 [ ^ ]

I've managed to zoom the entire relative layout using setPivotX(float), setPivotY(float), setScaleX(float), setScaleY(float). But, the behaviour of setPivotX(float) and setPivotY(float) is still confusing me. Actually I want to zoom the layout at the point on which user touch. So, the user touch point will be in the middle of the screen after the layout is scaled. So, I guess the setPivotX(float) and setPivotY(float) should be set to the touchPoint, but that doesn't work. Anyway here is my code:

main.xml

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:id="@+id/layout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#ffffff">
 <ImageView
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ic_launcher"
   android:visibility="invisible"/>
</RelativeLayout>



MainActivity.java

public class MainActivity extends Activity
{
  RelativeLayout layout;
  LayoutParams params;
  ImageView image;
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    image = (ImageView) findViewById(R.id.image);
    layout = (RelativeLayout) findViewById(R.id.layout);
    layout.setOnTouchListener(new TouchHandler());
  }
  private class TouchHandler implements OnTouchListener
  {
    @Override
    public boolean onTouch(View view, MotionEvent event)
    {
      if (event.getAction() == MotionEvent.ACTION_DOWN)
      {
        params.leftMargin = Math.round(event.getX() * 160f / getBaseContext().getResources().getDisplayMetrics().densityDpi);
        params.topMargin = Math.round(event.getY() * 160f / getBaseContext().getResources().getDisplayMetrics().densityDpi);
        image.setLayoutParams(params);
        image.setVisibility(View.VISIBLE);
        layout.setPivotX(event.getX());
        layout.setPivotY(event.getY());
        layout.setScaleX(2f);
        layout.setScaleY(2f);
      }
      return true;
    }
  }
}



Note:

I use ic_launcher to identify user touch point. So if the code works, the ic_launcher will be always in the middle of the screen.

解决方案

See:
Zooming a View[^]
http://developer.sonymobile.com/2010/05/18/android-one-finger-zoom-tutorial-part-1/[^]
Also:
pinch to zoom multi-touch feature in Android[^]


这篇关于基于触摸缩放RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆