【问题标题】:Android: How do I make a scrollable Canvas?Android:如何制作可滚动的画布?
【发布时间】:2018-02-12 01:41:34
【问题描述】:

我正在编写一个应用程序。它的一个方面是在图像上绘制线条。这是我一直在使用的练习代码:

公共类 DrawView 扩展视图 { 油漆油漆 = 新油漆();

private void init() {
    paint.setColor(Color.BLACK);
}

public DrawView(Context context) {
    super(context);
    init();
}

public DrawView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public DrawView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

@Override
public void onDraw(Canvas canvas) {
    paint.setStrokeWidth(20);
    canvas.drawLine(100, 100, 20, 20, paint);
    canvas.drawLine(20, 0, 0, 20, paint);

}

}

我希望 Canvas 相当大,因此我需要人们能够双向滚动(左/右和上/下)。我该如何做到这一点?我不熟悉 Canvas 类,因此将不胜感激。

【问题讨论】:

    标签: java android canvas drawable


    【解决方案1】:

    如果您想使用自定义高度和宽度在画布上绘图,您必须在活动类中调用 setContentView(android.view.View yourView, android.view.Viewgroup.LayoutParam yourLayout)。因为默认情况下 setContentView(View view ) 方法使用全宽和全高。所以你必须使用它的重载方法和两个参数以及你想要的。有关更多信息,请参阅文档。不要仅使用 LayoutParams() 构造函数来创建其对象。通过编写其完整路径来使用它,例如 android.view.ViewGroup.LayoutParams。因为Android SDK中还有其他一些同名的类。

    MyView customView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        customView = new MyView(getApplicationContext());
        android.view.ViewGroup.LayoutParams lp = new android.view.ViewGroup.LayoutParams(100,200);//100 is width and 200 is height
        setContentView(customView, lp);
        customView.setOnClickListener(this);
    
    }`  
    

    【讨论】:

      猜你喜欢
      • 2017-08-23
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多