在drawable文件夹中添加自己的my.xml文件,描述如:
然后再需要用的地方使用:
- Android:background="@drawable/mybg"
代码实现方式:
Java代码- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
- BitmapDrawable bd = new BitmapDrawable(bitmap);
- bd.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
- bd.setDither(true);
- view.setBackgroundDrawable(bd);
再着可以用*.9.png实现。
可以在上左两边添加要延伸的像素行。
在Android的UI开发中,有时为了界面美观而需要虚线。
- package com.custom;
- import Android.content.Context;
- import Android.graphics.Canvas;
- import Android.graphics.Color;
- import Android.graphics.DashPathEffect;
- import Android.graphics.Paint;
- import Android.graphics.Path;
- import Android.graphics.PathEffect;
- import Android.graphics.Rect;
- import Android.util.AttributeSet;
- import Android.util.DisplayMetrics;
- import Android.util.Log;
- import Android.view.View;
- /**
- * 画虚线组件
- * @author linweidong
- * @Date 2010-09-15
- */
- public class DashedLine extends View {
- private final String namespace = "http://com.smartmap.driverbook";
- private float startX;
- private float startY;
- private float endX;
- private float endY;
- private Rect mRect;
- public DashedLine(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- Paint paint = new Paint();
- paint.setStyle(Paint.Style.STROKE);
- paint.setColor(Color.DKGRAY);
- Path path = new Path();
- path.moveTo(0, 10);
- path.lineTo(480,10);
- PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);
- paint.setPathEffect(effects);
- canvas.drawPath(path, paint);
- }
XML代码 <!--EndFragment-->