如何通过触摸android来拖动图像?

如何通过触摸android来拖动图像?,第1张

概述参见英文答案>Touchanddragimageinandroid                                    2个如何通过触摸android来拖动图像?请帮我一个示例代码.解决方法:packagecom.examples.Touchmoveimage;importandroid.app.Activity;importandroi

参见英文答案 > Touch and drag image in android                                    2个
如何通过触摸androID来拖动图像?
请帮我一个示例代码.

解决方法:

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;            }        });    }}

效果很好!!

总结

以上是内存溢出为你收集整理的如何通过触摸android来拖动图像?全部内容,希望文章能够帮你解决如何通过触摸android来拖动图像?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102586.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存