首页 > 其他 > 详细

将子View画在父View的(x,y)处

时间:2015-01-12 02:12:30      阅读:261      评论:0      收藏:0      [点我收藏+]

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();
?}

}

?

将子View画在父View的(x,y)处

原文:http://wuxifu001.iteye.com/blog/2174793

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!