Android 사용자 정의 주마등 구현(SurfaceView)
5429 단어 SurfaceView
public class MarqueeTextSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private MyThread myThread;
private String mText = "";
private boolean isSurfaceValid = false;
private float textLength = 0f;//
private float viewWidth = 880.0f;//
private float step = 0f;//
private float y = 0f;//
private float temp_view_plus_text_length = 0.0f;//
private float temp_view_plus_two_text_length = 0.0f;//
private int speed = 10;
private Paint paint = null;//
public MarqueeTextSurfaceView(Context context) {
super(context);
}
public MarqueeTextSurfaceView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MarqueeTextSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
holder.setFixedSize(width, height);
}
public void setText(String str) {//
mText = str;
textLength = paint.measureText(mText);
temp_view_plus_two_text_length = viewWidth + textLength * 2f;
// viewWidth = mHolder.getSurfaceFrame().width();
step = textLength;
temp_view_plus_text_length = viewWidth + textLength;
}
public void setSpeed(int speed) {//
this.speed = speed;
}
public void canScroll() {
isSurfaceValid = true;
}
public void suspend() {//
step = temp_view_plus_text_length - 20;
}
public void init() {//
setZOrderOnTop(true);
mHolder = getHolder();
mHolder.addCallback(this);
myThread = new MyThread(mHolder);
mHolder.setFormat(PixelFormat.TRANSPARENT);
paint = new Paint(Paint.ANTI_ALIAS_FLAG); //
paint.setTextSize(spToPixel(getContext(), 20f));
paint.setColor(getResources().getColor(R.color.marquee));
y = getFontHeight(paint);
canScroll();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
isSurfaceValid = true;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isSurfaceValid = false;
}
class MyThread extends Thread {
private SurfaceHolder holder;
public boolean canRun;
public MyThread(SurfaceHolder holder) {
this.canRun = true;
this.holder = holder;
}
@Override
public void run() {
Canvas c = null;
while (canRun && isSurfaceValid) {
try {
// Thread.sleep(50);
c = holder.lockCanvas();// , Canvas, 。
handlerDrawInternal(c);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);// , 。
}
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}// 1
}
// reset
try {
c = holder.lockCanvas();
handlerDrawInternal(c);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
}
}
public void startScroll() {//
if (!isSurfaceValid) {
return;
}
// myThread.canRun = true;
if (!myThread.isAlive()) {
myThread = new MyThread(mHolder);
myThread.start();
}
}
public void stopScroll() {//
myThread.canRun = false;
isSurfaceValid = false;
// myThread.interrupt();
}
private void handlerDrawInternal(Canvas canvas) {
if (canvas == null) {
return;
}
step += speed;
if (step >= temp_view_plus_two_text_length)// ,
step = textLength;
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
canvas.drawText(mText, 0, mText.length(), temp_view_plus_text_length - step, y, paint);
}
private static float getFontHeight(Paint paint) {//
Rect bounds = new Rect();
paint.getTextBounds(" ", 0, 1, bounds);
return bounds.height();
}
// private static float pixelsToSp(Context context, Float px) {
// float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
// return px / scaledDensity;
// }
private static float spToPixel(Context context, Float sp) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return sp * scaledDensity;
}
}
개인 개발 소프트웨어 다운로드를 환영합니다:http://apk.91.com/Soft/Android/com.yln.game-3-1.1.html
응원해 주셔서 감사합니다!