由于移动设备屏幕通常比较小,可能不能满足工程中整个图形的显示,我找了很久就是没找到在surfaceView中实现canvas上下滚动。解决此问题我认为有两种思路,一种思路根据手的滑动,动态的绘制图形,即不断的重复清屏和绘制;另一种思路是借助ScrollView,设置surfaceView的高宽(注意:默认情况下surfaceView的高和宽即是canvas的高和宽,也可以自己修改)。第二种思路实现起来比较简单,本文实现了第二种思路如下:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SurfaceViewSlideActivity" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<SurfaceView android:id="@+id/surface_view" android:background="#FFFFFF" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.SurfaceView; import android.view.ViewGroup;
public class SurfaceViewSlideActivity extends AppCompatActivity {
private SurfaceView surfaceView = null;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_surface_view_slide);
// 获得surfaceView this.surfaceView = findViewById(R.id.surface_view);
// 获得屏幕的高度 DisplayMetrics metric = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(metric); int height = metric.heightPixels;
// 重置SurfaceView的高度 ViewGroup.LayoutParams params = this.surfaceView.getLayoutParams(); params.height = 2*height;
SurfaceViewSlide surfaceView = new SurfaceViewSlide(this, this.surfaceView);
} }
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PixelFormat; import android.view.SurfaceHolder; import android.view.SurfaceView;
/** * Create by RuiAiXinAn 2019/5/22 * Class description: */ public class SurfaceViewSlide extends SurfaceView implements SurfaceHolder.Callback{
private SurfaceHolder surfaceHolder = null; private Paint paint = null;
public SurfaceViewSlide(Context context, SurfaceView surfaceView) { super(context);
this.surfaceHolder = surfaceView.getHolder();
// 设置为透明,如果不设置为透明,则绘制的直线不显示 surfaceView.setZOrderOnTop(true); this.surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
// 设置画笔 this.paint = new Paint(); this.paint.setColor(Color.BLACK); this.paint.setStrokeWidth(2.0f); this.paint.setStyle(Paint.Style.STROKE); this.paint.setAntiAlias(true);
// 添加回调 this.surfaceHolder.addCallback(this); }
@Override public void surfaceCreated(SurfaceHolder holder) {
// 绘制直线 Canvas canvas = this.surfaceHolder.lockCanvas(); // 清除canvas上的图 // this.canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); canvas.drawLine(1,1,2000,2000, this.paint); this.surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override public void surfaceDestroyed(SurfaceHolder holder) {
} }