package my.android.CodeLayout; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.AbsoluteLayout; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.AbsoluteLayout.LayoutParams; public class CodeLayout extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); show_layout(); } private MyImageView[] miv = new MyImageView[9]; private LayoutParams param; private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; @SuppressWarnings("deprecation") public void show_layout() { AbsoluteLayout layout = new AbsoluteLayout(this); //layout.setOrientation(LinearLayout.VERTICAL); for (int i = 0; i < 9; i++) { miv[i] = new MyImageView(this, String.valueOf(i)); miv[i].setBackgroundColor(Color.BLUE); param = new AbsoluteLayout.LayoutParams(50, 50, 50, (50 * i)); layout.addView(miv[i], param); } setContentView(layout); } } package my.android.CodeLayout; import my.android.CodeLayout.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.graphics.drawable.BitmapDrawable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.widget.AbsoluteLayout; import android.widget.ImageView; public class MyImageView extends ImageView { public MyImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attr) { super(context, attr); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyImageView(Context context, String m) { super(context); mString = m; // TODO Auto-generated constructor stub } private Paint mPaint; //private Context mContext; private String mString = "Welcome to Mr Wei's blog"; protected void onCreate() { } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint = new Paint(); // 设置画笔颜色 //mPaint.setColor(Color.RED); // 设置填充 //mPaint.setStyle(Style.FILL); // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 //canvas.drawRect(new Rect(0, 0, 20, 20), mPaint); mPaint.setColor(Color.RED); mPaint.setTextSize(30); // 绘制文字 canvas.drawText(mString, 15, 35, mPaint); /* Rect src = new Rect(); Rect target = new Rect(); Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon); src.set(0, 0, bmp.getWidth(), bmp.getHeight()); target.set(0, 0, bmp.getWidth(), bmp.getHeight()); Paint paint = new Paint(); canvas.drawBitmap(bmp, src, target, paint); */ } public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); try { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: picMove(x, y); break; case MotionEvent.ACTION_UP: break; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return true; } private void picMove(float cX, float cY) { int x = (int) (super.getLeft() + (cX - 25)); int y = (int) (super.getTop() + (cY - 25)); this.setLayoutParams(new AbsoluteLayout.LayoutParams(50, 50, x, y)); } }