1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.os.Build; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import android.util.AttributeSet; import android.view.View; import com.zjkq.highnet_jt.R; import java.util.ArrayList; import java.util.List;
public class StarBgView extends View { private int bgColor; private int starColor; private int starQty; private Paint starPaint; private boolean hasInit; private List<StarData> stars; private static class StarData { private int alpha; private int x; private int y; private int color; private RectF rect; public RectF getRect() { return rect; } public void setRect(RectF rect) { this.rect = rect; } public int getAlpha() { return alpha; } public void setAlpha(int alpha) { this.alpha = alpha; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } } public StarBgView(Context context) { super(context); init(context, null); } public StarBgView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } public StarBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public StarBgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(context, attrs); } private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StarBgView); bgColor = a.getColor(R.styleable.StarBgView_sbv_bg_color, 0xff0a224a); starColor = a.getColor(R.styleable.StarBgView_sbv_star_color, 0xffffffff); starQty = a.getInteger(R.styleable.StarBgView_sbv_star_qty, 50); a.recycle(); } else { bgColor = 0xff0a224a; starColor = 0xffffffff; starQty = 50; } initPaint(); setBackgroundColor(bgColor); } private void generateStarData() { if (stars != null) { stars.clear(); stars = null; } stars = new ArrayList<>(); for (int i = 0; i < starQty; i++) { int x = getRandomX(); int y = getRandomY(); int alpha = getRandomAlpha(); StarData starData = new StarData(); starData.setX(x); starData.setY(y); starData.setAlpha(alpha); if (i % 2 == 0) { starData.setRect(new RectF(x, y, x + 1, y + 2)); } else { starData.setRect(new RectF(x, y, x + 2, y + 1)); }
stars.add(starData); } } private int getRandomAlpha() { return (int) (255 * Math.random() + 0.5f); } private int getRandomX() { return (int) (getWidth() * Math.random() + 0.5f); } private int getRandomY() { return (int) (getHeight() * Math.random() + 0.5f); } private void drawStar(Canvas canvas) { if (stars != null) { for (StarData star : stars) { starPaint.setAlpha(star.getAlpha()); canvas.drawRoundRect(star.getRect(), 1, 1, starPaint); } } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (stars == null) { generateStarData(); } drawStar(canvas); } private void initPaint() { starPaint = new Paint(); starPaint.setColor(starColor); starPaint.setStyle(Paint.Style.FILL); starPaint.setAntiAlias(true); starPaint.setStrokeWidth(1); } }
|