当前位置: 代码迷 >> Android >> Android 图片平铺成效
  详细解决方案

Android 图片平铺成效

热度:59   发布时间:2016-05-01 19:09:54.0
Android 图片平铺效果

在drawable文件夹中添加自己的my.xml文件,描述如:

  1. <?xml?version?="1.0"?encoding?="utf-8"??>????
  2. <bitmap????
  3. ????xmlns:Android="http://schemas.android.com/apk/res/android"????
  4. ????Android:src="@drawable/pic_bg"????
  5. ????Android:tileMode="repeat"?/>????

然后再需要用的地方使用:

Xml代码
  1. Android:background="@drawable/mybg"????

代码实现方式:

Java代码
  1. Bitmap?bitmap?=?BitmapFactory.decodeResource(getResources(),?R.drawable.icon);????
  2. BitmapDrawable?bd?=?new?BitmapDrawable(bitmap);????
  3. bd.setTileModeXY(TileMode.REPEAT?,?TileMode.REPEAT?);????
  4. bd.setDither(true);????
  5. view.setBackgroundDrawable(bd);???

再着可以用*.9.png实现。

可以在上左两边添加要延伸的像素行。

在Android的UI开发中,有时为了界面美观而需要虚线。

  1. package?com.custom;????
  2. ????
  3. ???import?Android.content.Context;????
  4. import?Android.graphics.Canvas;????
  5. import?Android.graphics.Color;????
  6. import?Android.graphics.DashPathEffect;????
  7. import?Android.graphics.Paint;????
  8. import?Android.graphics.Path;????
  9. import?Android.graphics.PathEffect;????
  10. import?Android.graphics.Rect;????
  11. import?Android.util.AttributeSet;????
  12. import?Android.util.DisplayMetrics;????
  13. import?Android.util.Log;????
  14. import?Android.view.View;????
  15. /**??
  16. ?*?画虚线组件??
  17. [email protected]?
  18. [email protected]?
  19. ?*/????
  20. public?class?DashedLine?extends?View?{????
  21. ????private?final?String?namespace?=?"http://com.smartmap.driverbook";????
  22. ????private?float?startX;????
  23. ????private?float?startY;????
  24. ????private?float?endX;????
  25. ????private?float?endY;????
  26. ????private?Rect?mRect;????
  27. ??????
  28. ????public?DashedLine(Context?context,?AttributeSet?attrs)?{????
  29. ????????super(context,?attrs);??????????????
  30. ????????????
  31. ????}????
  32. ????
  33. ????@Override????
  34. ????protected?void?onDraw(Canvas?canvas)?{????
  35. ????????//?TODO?Auto-generated?method?stub?????
  36. ????????super.onDraw(canvas);????????????
  37. ????????Paint?paint?=?new?Paint();????
  38. ????????paint.setStyle(Paint.Style.STROKE);????
  39. ????????paint.setColor(Color.DKGRAY);????
  40. ????????Path?path?=?new?Path();?????????
  41. ????????path.moveTo(0,?10);????
  42. ????????path.lineTo(480,10);??????????
  43. ????????PathEffect?effects?=?new?DashPathEffect(new?float[]{5,5,5,5},1);????
  44. ????????paint.setPathEffect(effects);????
  45. ????????canvas.drawPath(path,?paint);????
  46. ????}????
在layout文件夹下的xml引用这个控件
XML代码
  1. <com.custom.DashedLine?????
  2. ?????Android:id="@+id/dashedLine"????
  3. ?????Android:layout_width="wrap_content"?????????????
  4. ?????Android:layout_height="20px"??????????????????
  5. ????????????????????????
  6. ??????/>? ??
<!--EndFragment-->
  相关解决方案