gpt4 book ai didi

android - 如何将图像从一种布局拖到另一种布局

转载 作者:行者123 更新时间:2023-11-30 04:42:58 25 4
gpt4 key购买 nike

如何将图像从一种布局拖到另一种布局。我附上我到目前为止所做的事情。我在一个 View 中有两个布局。我可以在其初始布局上移动图像。但问题是我无法将图像移动到不同的布局。请帮我找到解决这个问题的办法。

    <?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"> <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:scaleType="matrix"
android:layout_weight="1">
</ImageView>

<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/icon"
android:scaleType="matrix"
android:layout_weight="1"
android:background="#00aa00"
>
</ImageView>


</LinearLayout>

</LinearLayout>

主文件是

package com.example.multitouch;
import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class multitouch extends Activity implements OnTouchListener{

/** Called when the activity is first created. */
Matrix matrix = new Matrix() ;
Matrix eventmatrix = new Matrix();
static float centerX,centerY;
final static int NONE = 0;
final static int DRAG = 1;
int touchState=NONE;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
ImageView view = (ImageView) v;
final int action = event.getAction();


switch(action){
case MotionEvent.ACTION_DOWN:
touchState= DRAG;
centerX= event.getX();
centerY = event.getY();
eventmatrix.set(matrix);
break;
case MotionEvent.ACTION_MOVE:
if (touchState == DRAG) {
matrix.set(eventmatrix);
matrix.setTranslate(event.getX()-centerX,event.getY()-centerY);
}
view.setImageMatrix(matrix);
//view.setImageMatrix(eventmatrix);
break;
case MotionEvent.ACTION_UP:
//touchState=NONE;
break;

}
return true;
}
}

请给我回复

最佳答案

它可能会变得非常复杂,但请检查一下: http://blahti.wordpress.com/2011/02/10/moving-views-part-3/

关于android - 如何将图像从一种布局拖到另一种布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5623445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com