您好,登錄后才能下訂單哦!
package com.example.ex_templete;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class ViewPagerIndicator extends View {
private Paint paint;
private Paint forePaint;
private float foreOffset;
private static final int RADIUS = 10;
private static int PIVOT_X = 30;
private static final int PIVOT_Y = 30;
private static final int LEN = 3 * RADIUS;
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = (7 - 1) * LEN + 2 * RADIUS;
int screenWidth = getScreenWidth();
PIVOT_X = (screenWidth - width) / 2;
for (int i = 0; i < 7; i++) {
canvas.drawCircle(PIVOT_X + i * 3 * RADIUS, PIVOT_Y, RADIUS, paint);
}
canvas.drawCircle(PIVOT_X + foreOffset, PIVOT_Y, RADIUS, forePaint);
}
// 得到屏幕的Width
private int getScreenWidth() {
WindowManager windowMgr = (WindowManager)
getContext().getSystemService
(Context.WINDOW_SERVICE);
Display display = windowMgr.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
return outMetrics.widthPixels;
}
public void updateOffset(int position, float positionOffset)
{
foreOffset = position % 7 * LEN
+ positionOffset * LEN;
invalidate();
}
public ViewPagerIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStyle(Style.STROKE);// 絀哄績
paint.setStrokeWidth(1);// 杈圭殑鍘氬害
forePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
forePaint.setColor(Color.BLUE);
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。