如何拖动屏幕上的图像 [英] How to drag images around the screen
本文介绍了如何拖动屏幕上的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能帮助我如何元的图像,可以在屏幕上拖动C $ C程序?你能给我一个样品code吧。非常感谢。以及如何做一个游戏分数吧。我将AP preciate你的回应。
解决方案
imageView.setOnLongClickListener(新OnLongClickListener(){ 公共布尔onLongClick(视图v){
modeEdit = FALSE;
ClipData.Item项目=新ClipData.Item((CharSequence的)v.getTag());
的String [] = MIMETYPES {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData =新ClipData(v.getTag()的toString(),MIME类型,项目); //实例化拖影建设者。
DragShadowBuilder myShadow =新MyDragShadowBuilder(treeBitmap);
v.startDrag(dragData,//要拖动的数据
myShadow //拖动阴影建设者
treeBitmap,//没有必要使用本地数据
0 //标志(当前未使用,设置为0)
);
返回true;
}
});
这code以下是设置监听拖
MyDragEventListener mDragListen =新MyDragEventListener();
这是拖累听众
保护类MyDragEventListener实现OnDragListener { 公共ondrag当布尔(视图V,事件的dragEvent){
最终诠释行动= event.getAction(); 开关(动作){ 案例DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG,FIELD ACTION_DRAG_STARTED);
如果(event.getClipDescription()。hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
v.invalidate();
返回(真);
}
其他{
返回(假);
} 案例DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG,FIELD ACTION_DRAG_ENTERED);
树presentInMyView = TRUE;
//Toast.makeText(getApplicationContext(),拖动数据进入Toast.LENGTH_LONG).show();
v.invalidate();
返回(真); 案例DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG,FIELD ACTION_DRAG_LOCATION);
位X = event.getX();
positionY = event.getY();
的LayoutParams = v.getLayoutParams();
返回(真); 案例DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG,FIELD ACTION_DRAG_EXITED);
modeEdit = FALSE;
树presentInMyView = FALSE;
//Toast.makeText(getApplicationContext(),拖动数据退出,Toast.LENGTH_LONG).show();
v.invalidate();
返回(真); 案例DragEvent.ACTION_DROP:
Log.d(TAG,FIELD ACTION_DROP);
。keyOfTree = event.getClipDescription()getLabel()的toString();
Log.d(TAG,KEY VALUE =+ keyOfTree); 最后ImageView的观点=新ImageView的(getApplicationContext());
串树宽= getResources()的getString(R.string.treeWidth)。
字符串treeHeight = getResources()的getString(R.string.treeHeight)。 RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(的Integer.parseInt(树宽)的Integer.parseInt(treeHeight));
view.setLayoutParams(PARAMS);
view.setImageResource(R.drawable.tree1); //获取图像的宽度和高度:
BitmapFactory.Options O =新BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
BMP位图= BitmapFactory.de codeResource(getResources(),R.drawable.tree1,O);
INT W = bmp.getWidth();
INT H = bmp.getHeight(); Log.d(TAG,宽度=+ W +高度=+ H);
params.leftMargin =(INT)位X - 瓦; // - W和-h所以拖图像放置在没有鼠标
params.topMargin =(INT)positionY - H; //但如果实际图像是在地图上 view.setVisibility(View.VISIBLE);
openField.addView(视图);
LinearLayout.LayoutParams paramsOpenField =新LinearLayout.LayoutParams(openFieldWidth,openFieldHeight);
openField.setLayoutParams(paramsOpenField); 尝试{
视频下载(1000);
}
赶上(InterruptedException的E){
e.printStackTrace();
}
v.invalidate();
返回true; 案例DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG,FIELD ACTION_DRAG_ENDED);
v.invalidate();
如果(event.getResult()及树presentInMyView){
Log.d(TAG,(X,Y)=(+位X +,+ positionY +));
INT subSubPlot = 0;
如果(位X> openField.measuredWidth / 2){// 3,4
如果(positionY> openField.measuredHeight / 2){
subSubPlot = 4;
}
其他{
subSubPlot = 3;
}
}
其他{
如果(positionY> openField.measuredHeight / 2){
subSubPlot = 1;
}
其他{
subSubPlot = 2;
}
}
双latitudeOfTree = mValues [0] +(Math.abs(mValues [0] - mValues [2])/ openField.measuredWidth)*位X;
双longitudeOfTree = mValues [1] +(Math.abs(mValues [1] - mValues [3])/ openField.measuredHeight)* positionY;
// openField.drawTreeCoordinated(latitudeOfTree,longitudeOfTree,位X,positionY); 返回true;
}
其他{
返回false;
} 默认:
Log.e(TAG,由OnDragListener收到未知的操作类型。);
打破;
};
返回false;
}
};
Can anyone help me on how to code program for an image that can be drag around the screen? Can you give me a sample code for it. thanks a lot. And how can i make a game score for it. i will appreciate your response.
解决方案
imageView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
modeEdit = false;
ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);
// Instantiates the drag shadow builder.
DragShadowBuilder myShadow = new MyDragShadowBuilder(treeBitmap);
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
treeBitmap, // no need to use local data
0 // flags (not currently used, set to 0)
);
return true;
}
});
This code below is to set the drag listener
MyDragEventListener mDragListen = new MyDragEventListener();
This is the drag listener
protected class MyDragEventListener implements OnDragListener {
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(TAG, "FIELD ACTION_DRAG_STARTED");
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
v.invalidate();
return(true);
}
else {
return(false);
}
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(TAG, "FIELD ACTION_DRAG_ENTERED");
treePresentInMyView = true;
//Toast.makeText(getApplicationContext(), "Dragged data entered", Toast.LENGTH_LONG).show();
v.invalidate();
return(true);
case DragEvent.ACTION_DRAG_LOCATION:
Log.d(TAG, "FIELD ACTION_DRAG_LOCATION");
positionX = event.getX();
positionY = event.getY();
layoutParams = v.getLayoutParams();
return(true);
case DragEvent.ACTION_DRAG_EXITED:
Log.d(TAG, "FIELD ACTION_DRAG_EXITED");
modeEdit = false;
treePresentInMyView = false;
//Toast.makeText(getApplicationContext(), "Dragged data exited", Toast.LENGTH_LONG).show();
v.invalidate();
return(true);
case DragEvent.ACTION_DROP:
Log.d(TAG, "FIELD ACTION_DROP");
keyOfTree = event.getClipDescription().getLabel().toString();
Log.d(TAG, "KEY VALUE = "+keyOfTree);
final ImageView view = new ImageView(getApplicationContext());
String treeWidth = getResources().getString(R.string.treeWidth);
String treeHeight = getResources().getString(R.string.treeHeight);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Integer.parseInt(treeWidth), Integer.parseInt(treeHeight));
view.setLayoutParams(params);
view.setImageResource(R.drawable.tree1);
//to get image width and height:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tree1, o);
int w = bmp.getWidth();
int h = bmp.getHeight();
Log.d(TAG, "width = " + w + " height = " +h);
params.leftMargin = (int) positionX - w;//-w and -h so dragged image placed not at mouse
params.topMargin = (int) positionY - h;// but where actual image is on map
view.setVisibility(View.VISIBLE);
openField.addView(view);
LinearLayout.LayoutParams paramsOpenField = new LinearLayout.LayoutParams(openFieldWidth, openFieldHeight);
openField.setLayoutParams(paramsOpenField);
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
v.invalidate();
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(TAG, "FIELD ACTION_DRAG_ENDED");
v.invalidate();
if (event.getResult() & treePresentInMyView) {
Log.d(TAG, "(x,y) = ("+positionX+", "+positionY+")");
int subSubPlot = 0;
if (positionX > openField.measuredWidth/2) { //3 and 4
if (positionY > openField.measuredHeight/2) {
subSubPlot = 4;
}
else {
subSubPlot = 3;
}
}
else {
if (positionY > openField.measuredHeight/2) {
subSubPlot = 1;
}
else {
subSubPlot = 2;
}
}
double latitudeOfTree = mValues[0] + (Math.abs(mValues[0] - mValues[2]) / openField.measuredWidth) * positionX;
double longitudeOfTree = mValues[1] + (Math.abs(mValues[1] - mValues[3])/ openField.measuredHeight) * positionY;
// openField.drawTreeCoordinated(latitudeOfTree, longitudeOfTree, positionX, positionY);
return true;
}
else {
return false;
}
default:
Log.e(TAG,"Unknown action type received by OnDragListener.");
break;
};
return false;
}
};
这篇关于如何拖动屏幕上的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文