package com.control;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAbsoluteLayout extends AbsoluteLayout {
private static final String TAG=MyAbsoluteLayout.class.getSimpleName();
private Bitmap bitmap,bitmap_push,bitmap_ball;
private ImageView iv=null;
private ImageView iv_ball=null;
private int locX=50,locX_ball=250;
private int locY=50,locY_ball=250;
private static int buf[]={R.drawable.a,R.drawable.b,R.drawable.ball,R.drawable.a_press,R.drawable.b_press};
private static int bufSizeX[]={50,100,150,200,250};
int n=0;
GestureDetector mGestureDetector;
AbsoluteLayout.LayoutParams params,params_ball;
public MyAbsoluteLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.layout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT, 0, 0);
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
bitmap_push=BitmapFactory.decodeResource(getResources(), R.drawable.add_bt);
bitmap_ball=BitmapFactory.decodeResource(getResources(), R.drawable.ball);
for(int i=0;i<buf.length;i++){
iv=new ImageView(this.getContext());
bitmap=BitmapFactory.decodeResource(getResources(), buf[i]);
params=new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT, bufSizeX[i], bufSizeX[i]);
iv.setImageBitmap(bitmap);
this.addView(iv,params);
iv.setOnTouchListener(onl);
}
// iv=new ImageView(this.getContext());
// params=new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT, locX, locY);
// iv.setImageBitmap(bitmap);
// this.addView(iv,params);
// TextView tx=new TextView(this.getContext());
// params=new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT, 0, 0);
// this.addView(tx,params);
// //ball
// iv_ball=new ImageView(this.getContext());
// params_ball=new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT, locX_ball, locY_ball);
// iv_ball.setImageBitmap(bitmap_ball);
// this.addView(iv_ball,params_ball);
// iv.setOnTouchListener(onl);
// iv_ball.setOnTouchListener(onl);
// this.setOnTouchListener(onl);
}
private OnTouchListener onl=new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int x=(int)event.getRawX();
int y=(int)event.getRawY();
if(event.getAction() == MotionEvent.ACTION_DOWN){
locX=x;
locY=y;
Log.d(TAG, “down—locX”+x+”locY”+y);
if(v == iv){
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.add_bt);
iv.setImageBitmap(bitmap);
}
}
if(event.getAction() == MotionEvent.ACTION_UP){
locX=x;
locY=y;
Log.d(TAG, “up—locX”+x+”locY”+y);
if(v == iv){
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
iv.setImageBitmap(bitmap);
}
if(v == iv_ball){
iv_ball.layout(locX_ball, locY_ball, locX_ball+iv_ball.getWidth(), locY_ball+iv_ball.getHeight());
}
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
locX=x;
locY=y;
if(v == iv_ball){
// locX_ball=locX;
// locY_ball=locY;
Log.d(TAG, “move—locX”+x+”locY”+y);
iv_ball.layout(locX – iv_ball.getWidth() / 2, locY – iv_ball.getHeight()
/ 2, locX + iv_ball.getWidth() / 2, locY + iv_ball.getHeight()
/ 2);
// iv_ball.layout(locX – MyAbsoluteLayout.this.getWidth() / 2, locY – MyAbsoluteLayout.this.getHeight()
// / 2, locX + MyAbsoluteLayout.this.getWidth() / 2, locY + MyAbsoluteLayout.this.getHeight()
// / 2);
//
//
// // slow down the motion reaction
// if (n % 4 == 0) {
// if (x < locX_ball && y > locY_ball
// && y < locY_ball + iv_ball.getWidth()) {
// System.out.println(“test] left”);
// } else if (x > locX_ball + iv_ball.getWidth() && y > locY_ball
// && y < locY_ball + iv_ball.getWidth()) {
// System.out.println(“test] right”);
// } else if (y < locY_ball && x > locX_ball
// && x < locX_ball + iv_ball.getWidth()) {
// System.out.println(“test] top”);
// } else if (y > locY_ball + iv_ball.getWidth() && x > locX_ball
// && x < locX_ball + iv_ball.getWidth()) {
// System.out.println(“test] buttom”);
// }
// }
}
}
v.postInvalidate();
return true;
}
};
}
—————————–
<com.control.MyAbsoluteLayout android:id=”@+id/control_custom_view”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”/>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37552.html