java 生成 imageview,android 动态创建imageView,拖动imageView到任意位置

android

动态创建imageView,拖动imageView到任意位置(android2.1工程)

运行结果如图:

a4c26d1e5885305701be709a3d33442f.png

这里只实现R.drawable.light对象的拖动,其他对象操作方法一样:

.XML文件

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/root"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

android:id="@+id/linearLayout1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true" >

android:id="@+id/textView1"

android:layout_width="30dp"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:text="当前节点" />

android:layout_width="wrap_content"

android:layout_height="fill_parent" >

android:id="@+id/RelativeLayout2"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:id="@+id/imageView1"

android:layout_width="30dp"

android:layout_height="20dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/light" />

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/imageView1"

android:text="0"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="电灯节点"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:id="@+id/ImageView01"

android:layout_width="30dp"

android:layout_height="20dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/fog" />

android:id="@+id/TextView01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/ImageView01"

android:text="0"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:id="@+id/TextView02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="烟雾节点"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:layout_height="wrap_content">

android:layout_width="30dp"

android:layout_height="20dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/rain" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/ImageView02"

android:text="0"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="雨滴节点"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:id="@+id/ImageView03"

android:layout_width="30dp"

android:layout_height="20dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/thermometer" />

android:id="@+id/TextView05"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/ImageView03"

android:text="0"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:id="@+id/TextView06"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="温度节点"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:id="@+id/ImageView04"

android:layout_width="30dp"

android:layout_height="20dp"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/poisiongas" />

android:id="@+id/TextView08"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_toRightOf="@+id/ImageView04"

android:text="0"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:id="@+id/TextView07"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentLeft="true"

android:text="有害气体"

android:textAppearance="?android:attr/textAppearanceSmall"

/>

android:id="@+id/parentcontent"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentLeft="true"

android:layout_below="@+id/linearLayout1" >

android:id="@+id/imageView2"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:src="@drawable/home_back" />

java代码:

package com.example.testandroid;

import android.os.Bundle;

import android.app.Activity;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnTouchListener;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.RelativeLayout.LayoutParams;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

DisplayMetrics dm =

getResources().getDisplayMetrics();

final int screenWidth =

dm.widthPixels;

final int screenHeight =

dm.heightPixels - 50;//获取屏幕高度

ImageView home = (ImageView)

findViewById(R.id.imageView1);

final OnTouchListener

child;

child = new

OnTouchListener() {//对新创建的image进行监听

int lastX,

lastY;

public

boolean onTouch(View v, MotionEvent event) {

//

TODO Auto-generated method stub

int

action = event.getAction();

switch

(action) {

case

MotionEvent.ACTION_DOWN:

lastX

= (int) event.getRawX();//获取事件发生时的坐标

lastY

= (int) event.getRawY();

break;

case

MotionEvent.ACTION_MOVE:

int

dx = (int) event.getRawX() - lastX;

int

dy = (int) event.getRawY() - lastY;

int

left = v.getLeft() + dx;

int

top = v.getTop() + dy;

int

right = v.getRight() + dx;

int

bottom = v.getBottom() + dy;

if

(left < 0) {

left

= 0;

right

= left + v.getWidth();

}

if

(right > screenWidth) {

right

= screenWidth;

left

= right - v.getWidth();

}

if

(top < 0) {

top

= 0;

bottom

= top + v.getHeight();

}

if

(bottom > screenHeight) {

bottom

= screenHeight;

top

= bottom - v.getHeight();

}

v.layout(left,

top, right, bottom);//将移动后的图相在新位置显示出来

Log.i("",

"position:" + left + ", " + top + ", " + right

+

", " + bottom);

((RelativeLayout.LayoutParams)

(v.getLayoutParams())).leftMargin = left;

((RelativeLayout.LayoutParams)

(v.getLayoutParams())).topMargin = top;

lastX

= (int) event.getRawX();

lastY

= (int) event.getRawY();

break;

case

MotionEvent.ACTION_UP:

break;

}

return

true;

}

};

home.setOnTouchListener(new

OnTouchListener() {

int lastX,

lastY;

ImageView

image;

LayoutParams

params;

int

left;

int

top;

int

right;

int

bottom;

public

boolean onTouch(View v, MotionEvent event) {

int

ea = event.getAction();

Log.i("TAG",

"Touch:" + ea);

switch

(ea) {

case

MotionEvent.ACTION_DOWN:

RelativeLayout

parentcontener = (RelativeLayout)

findViewById(R.id.parentcontent);

lastX

= (int) event.getRawX();

lastY

= (int) event.getRawY();

ImageView

parent = (ImageView) findViewById(R.id.imageView1);

params

= new LayoutParams(parent.getWidth(), parent

.getHeight());

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

image

= new ImageView(MainActivity.this);

image.setLayoutParams(params);

parentcontener.addView(image);

image.setImageResource(R.drawable.light);

image.setOnTouchListener(child);

break;

case

MotionEvent.ACTION_MOVE:

int

dx = (int) event.getRawX() - lastX;

int

dy = (int) event.getRawY() - lastY;

left

= image.getLeft() + dx;

top

= image.getTop() + dy;

right

= image.getRight() + dx;

bottom

= image.getBottom() + dy;

if

(left < 0) {

left

= 0;

right

= left + image.getWidth();

}

if

(right > screenWidth) {

right

= screenWidth;

left

= right - image.getWidth();

}

if

(top < 0) {

top

= 0;

bottom

= top + image.getHeight();

}

if

(bottom > screenHeight) {

bottom

= screenHeight;

top

= bottom - image.getHeight();

}

image.layout(left,

top, right, bottom);

Log.i("",

"position:" + left + ", " + top + ", " + right

+

", " + bottom);

lastX

= (int) event.getRawX();

lastY

= (int) event.getRawY();

params.leftMargin=left;

params.topMargin=top;

break;

case

MotionEvent.ACTION_UP:

//

params.setMargins(left, top, right,

bottom);加上这句话,运行的时候可能出现小bug,但不是

//每次都出现,可测试一下

break;

}

return

true;

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值