【问题标题】:How to create image movement with functionality?如何使用功能创建图像移动?
【发布时间】:2014-05-30 08:39:06
【问题描述】:

我正在尝试创建类似于 android 的解锁活动的东西,您可以将锁定图像拖到圆圈外以解锁手机。我想拖动图像并在到达某个位置时开始活动。我试着用画布来做,但结果是画了我屏幕的其他部分。所以我尝试使用我在另一篇文章中找到的以下 sn-p:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_touch_ball);



    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;
                                                        layoutParams.topMargin = y_cord;

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

但这也不是很好,因为当我触摸图像时,它会改变它的大小并且在屏幕上也不会平滑移动。

你知道我如何启动一个活动,比如当我将图像从屏幕底部拖到顶部时?

【问题讨论】:

    标签: android image drag


    【解决方案1】:

    这是因为下面一行

    layoutParams.leftMargin = x_cord;
    layoutParams.topMargin = y_cord;
    balls.setLayoutParams(layoutParams);
    

    如果你正在设置边距,球会根据那个移动。

    【讨论】:

    • 那你有什么建议?我怎样才能让它变得更好?
    猜你喜欢
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多