如何通过触摸的Andr​​oid拖动图像?拖动、图像、Andr、oid

2023-09-12 07:38:56 作者:单人循环つ

如何通过触摸的Andr​​oid拖动图像? 请帮我一个样品code。

解决方案

 包com.examples.Touchmoveimage;

进口android.app.Activity;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.ImageView;
进口android.widget.LinearLayout.LayoutParams;

公共类Touchmoveimage延伸活动{

    INT WINDOWWIDTH;
    INT WINDOWHEIGHT;

    私人的LayoutParams的LayoutParams;
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);

    。WINDOWWIDTH = getWindowManager()getDefaultDisplay()的getWidth()。
    。WINDOWHEIGHT = getWindowManager()getDefaultDisplay()的getHeight();
    最后ImageView的球=(ImageView的)findViewById(R.id.ball);

    balls.setOnTouchListener(新View.OnTouchListener(){

                    @覆盖
                    公共布尔onTouch(视图V,MotionEvent事件){
                        的LayoutParams的LayoutParams =(的LayoutParams)balls.getLayoutParams();
                        开关(event.getAction())
                        {
                        案例MotionEvent.ACTION_DOWN:
                                                        打破;
                        案例MotionEvent.ACTION_MOVE:
                                                        INT x_cord =(INT)event.getRawX();
                                                        INT y_cord =(INT)event.getRawY();

                                                        如果(x_cord> WINDOWWIDTH){x_cord = WINDOWWIDTH;}
                                                        如果(y_cord> WINDOWHEIGHT){y_cord = WINDOWHEIGHT;}

                                                        layoutParams.leftMargin = x_cord -25;
                                                        layoutParams.topMargin = y_cord  -  75;

                                                        balls.setLayoutParams(的LayoutParams);
                                                        打破;
                        默认:
                                                        打破;
                        }
                            返回true;
                    }
            });
}
}
 

工作好!

how to drag an image by touching in android? Please Help me with a sample code.

解决方案

package com.examples.Touchmoveimage;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class Touchmoveimage extends Activity {

    int windowwidth;
    int windowheight;

    private LayoutParams layoutParams ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    windowwidth = getWindowManager().getDefaultDisplay().getWidth();
    windowheight = getWindowManager().getDefaultDisplay().getHeight();
    final ImageView balls = (ImageView)findViewById(R.id.ball);

    balls.setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        LayoutParams layoutParams = (LayoutParams) balls.getLayoutParams();
                        switch(event.getAction())
                        {
                        case MotionEvent.ACTION_DOWN:   
                                                        break;
                        case MotionEvent.ACTION_MOVE:
                                                        int x_cord = (int)event.getRawX();
                                                        int y_cord = (int)event.getRawY();

                                                        if(x_cord>windowwidth){x_cord=windowwidth;}
                                                        if(y_cord>windowheight){y_cord=windowheight;}

                                                        layoutParams.leftMargin = x_cord -25;
                                                        layoutParams.topMargin = y_cord - 75;

                                                        balls.setLayoutParams(layoutParams);
                                                        break;
                        default:
                                                        break;
                        }
                            return true;
                    }
            });
}
}

works good!!