lerayZhang 2015-01-11
package com.example.tttt;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
/**
* @author Administrator 将子View画在父View的(x,y)处
*
*/
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TextView textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView.setText("hlldfdf");
textView.setBackgroundColor(Color.RED);
System.out.println("measure前"+textView.getMeasuredWidth()+"/"+textView.getMeasuredHeight()+"/"+textView.getWidth()+"/"+textView.getHeight());
//不能少..........,不然textView.getWidth()==0,textView.getHeight()=0
textView.measure(textView.getMeasuredWidth(),
textView.getMeasuredHeight());
System.out.println("measure后"+textView.getMeasuredWidth()+"/"+textView.getMeasuredHeight()+"/"+textView.getWidth()+"/"+textView.getHeight());
textView.layout(0, 0, textView.getMeasuredWidth(),
textView.getMeasuredHeight());
System.out.println("layout后"+textView.getMeasuredWidth()+"/"+textView.getMeasuredHeight()+"/"+textView.getWidth()+"/"+textView.getHeight());
//1.将子View转成bitmap=》drawBitmap
//频繁生成bitmap容易内存溢出.............
Bitmap bitmap = Bitmap.createBitmap(textView.getWidth(),
textView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canva = new Canvas(bitmap);
textView.draw(canva);
Paint paint = new Paint();
canvas.drawBitmap(bitmap, 0, 100, paint);
System.out.println("最后"+textView.getMeasuredWidth()+"/"+textView.getMeasuredHeight()+"/"+textView.getWidth()+"/"+textView.getHeight());
//2.画布移动到指定位置...........
//2.canvas.save()=>canva.translate(x,y)=>textView.draw(canvas)=>canvas.restore();
canvas.save();
canva.translate(0, 500);
textView.draw(canvas);
canvas.restore();
}
}
package com.example.testviewpager;
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.RectF;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.RED);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.cat);
Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF dst = new RectF(400, 400, 400 + bitmap.getWidth(),
400 + bitmap.getHeight());
//完整图片画出,坐标在(400,400)
canvas.drawBitmap(bitmap, src, dst, paint);
RectF dst2 = new RectF(400, 800, 400 + bitmap.getWidth() / 2,
800 + bitmap.getHeight() / 2);
//完整图片画出,坐标在(400,800),只是变小了,变成原图的一半大
canvas.drawBitmap(bitmap, src, dst2, paint);
//从原图的(0,50)这个点开始裁剪图片
//从原图的(0,50)这个点开始裁剪图片
//从原图的(0,50)这个点开始裁剪图片
Rect src3 = new Rect(0, 50, bitmap.getWidth(), bitmap.getHeight());
RectF dst3 = new RectF(800, 400, 800 + bitmap.getWidth()-0,
400 + bitmap.getHeight()-50);
canvas.drawBitmap(bitmap, src3, dst3, paint);
}
}