当前位置:操作系统 > 安卓/Android >>

Android开发_拖动效果,旋转效果,两点放大缩小的实现

[java] 

最近在做一个项目,需要用到拖动图片,旋转效果,两点放大缩小。

 


首先拖动图片


布局,主要就是一个button做测试

[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/hello" 
    /> 
    <Button 
    android:id="@+id/btn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/bt" 
    />     
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
    <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/bt"
    />   
</LinearLayout>

 


[java]
package com.oppo.examples; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.Button; 
import android.widget.Toast; 
 
public class DraftTest extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        DisplayMetrics dm = getResources().getDisplayMetrics(); 
        final int screenWidth = dm.widthPixels; 
        final int screenHeight = dm.heightPixels - 50; 
 
        final Button b=(Button)findViewById(R.id.btn); 
         
        b.setOnTouchListener(new OnTouchListener(){ 
 
            int lastX, lastY; 
 
            @Override 
            public boolean onTouch(View v, MotionEvent event) { 
                // TODO Auto-generated method stub  
                int ea=event.getAction(); 
                Log.i("TAG", "Touch:"+ea); 
 
                //Toast.makeText(DraftTest.this, "位置:"+x+","+y, Toast.LENGTH_SHORT).show();  
 
                switch(ea){ 
                case MotionEvent.ACTION_DOWN: 
                    lastX = (int) event.getRawX(); 
                    lastY = (int) event.getRawY(); 
                    break; 
                    /**
                     * layout(l,t,r,b)
                     * l  Left position, relative to parent 
                    t  Top position, relative to parent 
                    r  Right position, relative to parent 
                    b  Bottom position, relative to parent  
                     * */ 
                case MotionEvent.ACTION_MOVE: 
                    int dx =(int)event.getRawX() - lastX; 
                    int dy =(int)event.getRawY() - lastY; 
                 
                    int left = v.getLeft() + dx; 
                    int top = v.getTop() + dy; 
                    int right = v.getRight() + dx; 
                    int bottom = v.getBottom() + dy; 
               

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,