SurfaceView实现canvas中的图形上下滚动

由于移动设备屏幕通常比较小,可能不能满足工程中整个图形的显示,我找了很久就是没找到在surfaceView中实现canvas上下滚动。解决此问题我认为有两种思路,一种思路根据手的滑动,动态的绘制图形,即不断的重复清屏和绘制;另一种思路是借助ScrollView,设置surfaceView的高宽(注意:默认情况下surfaceView的高和宽即是canvas的高和宽,也可以自己修改)。第二种思路实现起来比较简单,本文实现了第二种思路如下:

1、图形

      
2、源代码

 

xml文件


<?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>

SurfaceViewSlideActivity文件

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);

    }
}

SurfaceViewSlide文件

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) {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值