我已经开发了类似的实现来测试我是否应该使用视图或SurfaceView。我实现了视图,如下所示
public class TimerView extends View {
private Paint mPiePaint;
private RectF mShadowBounds;
private float diameter;
int startCount = 0;
private PanelThread thread;
public TimerView(Context context) {
super(context);
init();
}
public TimerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TimerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public TimerView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPiePaint.setStyle(Paint.Style.FILL);
mPiePaint.setColor(0xff000000);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Account for padding
float xpad = (float)(getPaddingLeft() + getPaddingRight());
float ypad = (float)(getPaddingTop() + getPaddingBottom());
float ww = (float)w - xpad;
float hh = (float)h - ypad;
// Figure out how big we can make the pie.
diameter = Math.min(ww, hh);
mShadowBounds = new RectF(0, 0, diameter, diameter);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (startCount == 360) startCount= 0;
canvas.drawArc(mShadowBounds,
0, startCount, true, mPiePaint);
invalidate();
startCount++;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int w = resolveSizeAndState(minw, widthMeasureSpec, 1);
int h = resolveSizeAndState(MeasureSpec.getSize(w), heightMeasureSpec, 0);
setMeasuredDimension(w, h);
}
}
我实现了我的Surface View,如下
public class TimerSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Paint mPiePaint;
private RectF mShadowBounds;
private float diameter;
int startCount = 0;
private PanelThread thread;
public TimerSurfaceView(Context context) {
super(context);
init();
}
public TimerSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TimerSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public TimerSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
getHolder().addCallback(this);
mPiePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPiePaint.setStyle(Paint.Style.FILL);
mPiePaint.setColor(0xff000000);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Account for padding
float xpad = (float)(getPaddingLeft() + getPaddingRight());
float ypad = (float)(getPaddingTop() + getPaddingBottom());
float ww = (float)w - xpad;
float hh = (float)h - ypad;
// Figure out how big we can make the pie.
diameter = Math.min(ww, hh);
mShadowBounds = new RectF(0, 0, diameter, diameter);
}
protected void drawSomething(Canvas canvas) {
canvas.drawColor(0xFFEEEEEE);
if (startCount == 360) startCount= 0;
canvas.drawArc(mShadowBounds,
0, startCount, true, mPiePaint);
startCount++;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int minw = getPaddingLeft() + getPaddingRight() + getSuggestedMinimumWidth();
int w = resolveSizeAndState(minw, widthMeasureSpec, 1);
int h = resolveSizeAndState(MeasureSpec.getSize(w), heightMeasureSpec, 0);
setMeasuredDimension(w, h);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
thread = new PanelThread(getHolder(), this); //Start the thread that
thread.setRunning(true); //will make calls to
thread.start(); //onDraw()
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// tell the thread to shut down and wait for it to finish
// this is a clean shutdown
boolean retry = true;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// try again shutting down the thread
}
}
}
}
和SurfaceView线程如下所示
class PanelThread extends Thread {
private SurfaceHolder surfaceHolder;
private TimerSurfaceView panel;
private boolean starRunning = false;
public PanelThread(SurfaceHolder surfaceHolder, TimerSurfaceView panel) {
this.surfaceHolder = surfaceHolder;
this.panel = panel;
}
public void setRunning(boolean run) { //Allow us to stop the thread
starRunning = run;
}
@Override
public void run() {
Canvas c;
while (starRunning) { //When setRunning(false) occurs, starRunning is
c = null; //set to false and loop ends, stopping thread
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
//Insert methods to modify positions of items in onDraw()
panel.drawSomething(c);
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
结果显示,如https://www.youtube.com/watch?v=s9craUgY3I4中所示,自定义视图(TimerView)比曲面视图更平滑。根据http://stackoverflow.com/questions/23893266/why-surfaceview-is-slower-than-a-custom-view
的说法,SurfaceView虽然速度较慢,但应该更流畅。
会不会是因为在SurfaceView中,我需要重新着色来擦除之前的draw canvas.drawColor(0xFFEEEEEE);
on drawSomething
函数?有没有一种方法可以消除重新着色的需要,就像我在TimerView中所做的那样,我只是在onDraw
中invalidate()
它
我面临的另一个问题是,当应用程序转到后台并返回时,TimerSurfaceView的drawSomthing
将收到一个空画布,而TimerView onDraw()不会失效,动画只是停止。我需要做些什么才能让它继续保持原样?
发布于 2016-04-01 18:03:10
正如我在回答中提到的--对你链接到的问题进行评论:
渲染到SurfaceView的
Canvas不是硬件加速的,而渲染到普通视图的Canvas是硬件加速的。
随着显示像素数的增加(由于小型设备上4K显示器的不可避免的驱动),软件渲染变得更慢。CPU性能和内存带宽的增加将抵消这一影响,但它在某些设备上表现不佳。
你可以通过各种方式来弥补这一点,例如using setFixedSize()
to limit the pixel count,但硬件加速渲染通常是一种更好的方法。
如果你的帧率受到CPU的限制,那么任何想要使用同一个CPU核心的东西都会导致jank。您可以将SurfaceView渲染器放在单独的线程中,这一事实很有帮助,但如果您正在突破设备的限制,那么这就无关紧要了。显示正在以一定的速率更新,如果您不能始终满足截止日期,那么您的动画将不会流畅。(其他一些想法可以在this appendix中找到。)
发布于 2016-07-29 12:01:29
Android开发人员说,你应该在主线程上做所有的动画,因为他们的框架在UI工作中不能很好地工作
https://stackoverflow.com/questions/36356748
复制