首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 平面设计 > 图形图像 >

android 中作图的图形与其他控件一起存在

2013-08-04 
android 中绘制的图形与其他控件一起存在public class MainActivity extends Activity {??? public void o

android 中绘制的图形与其他控件一起存在

public class MainActivity extends Activity {
??? public void onCreate(Bundle b) {
??? ??? super.onCreate(b);
??? ??? MyView mv = new MyView(this);
??? ??? LinearLayout layout = new LinearLayout(this);
??????? layout.setOrientation(LinearLayout.VERTICAL);
??????? Button bt1 = new Button(this);
??????? setContentView(layout);
??????? bt1.setText("按钮1");
?????? layout.addView(bt1);
?????? layout.addView(mv);
??? }??
}

?

public class MyView extends View {
??? public MyView(Context context) {
??? ??? super(context);
??? }

??? protected void onDraw(Canvas canvas) {
??? ??? DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
??? ??? int h = displayMetrics.heightPixels;
??? ??? int w = displayMetrics.widthPixels;
??? ???
??? ??? super.onDraw(canvas);
??? ??? /* 设置背景为白色 */
??? ??? canvas.drawColor(Color.WHITE);
??? ??? Paint paint = new Paint();
??? ??? /* 去锯齿 */
??? ??? /* 设置paint的颜色 */
??? ??? paint.setColor(Color.RED);
??? ??? /* 设置paint的 style 为STROKE:空心 */
??? ??? paint.setStyle(Paint.Style.STROKE);
??? ??? /* 设置paint的外框宽度 */
??? ??? paint.setStrokeWidth(2);
??? ??? Rect rect = new Rect();
??? ??? rect.left = (w/2) - 100;
??? ??? rect.right = (w/2) + 100;
??? ??? rect.top = (h/2) - 200;
??? ??? rect.bottom = (h/2) + 200;
???
??? ??? /* 画一个空心长方形 */
??? ??? canvas.drawRect(rect, paint);
??? ??? canvas.drawText(getResources().getString(R.string.str_text1), (w/2)-20, (h/2)-50, paint);
??? ???
??? ??? /* 写字 */
??? ??? paint.setTextSize(24);
??? }
}

说明:

R.string.str_text1在string.xml里面自己定义就好了

这个程序可以使绘制的图形与其他控件一起存在

热点排行