Android 开发实例:图片拖动的实现(java)
2010-02-03 16:32:00 来源:WEB开发网package org.anjoy.activity;
import *.*;
// 图片拖动示例
public class MoveImageViewSample extends Activity {
private int screenWidth, screenHeight;
// ImageView 图片显示区域
private ImageView iv_1;
// TextView 显示屏幕,图片位置等信息
private TextView tv_1, tv_2, tv_3;
// 图片的位置参数对象,
// 通过Drawable对象的getLayoutParams()方法取得,可得到图片的位置信息
// 通过设置LayoutParams方法的x,y坐标,并传给图片,可以改变图片的位置,如:
// setLayoutParams(paramsIv_1)
private AbsoluteLayout.LayoutParams paramsIv_1;
// 记录图片的长宽,与左上角一点的坐标
private int picWidth, picHeight, picX, picY;
// 在onTouchEvent方法中设置,标记是否击中图片
private boolean clickPic;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 加载layout
setContentView(R.layout.moveimageviewsample);
// 得到屏幕分辨率,并在text view 1 上显示
tv_1 = (TextView) this.findViewById(R.id.TextInfo_1);
screenWidth = AnjoyUtil.getScreenWidth(this);
screenHeight = AnjoyUtil.getScreenHight(this);
tv_1.setText("Anjoy Screen information" + screenHeight + ":"
+ screenWidth);
// 加载图片,在image view 1上显示
Drawable pic = getResources().getDrawable(R.drawable.icon);
iv_1 = (ImageView) this.findViewById(R.id.ImageView01);
iv_1.setImageDrawable(pic);
// iv_1.setBackgroundColor(Color.LTGRAY);
iv_1.setScaleType(ScaleType.FIT_CENTER);
更多精彩
赞助商链接