通过&QUOT画在画布上的位图

通过&QUOT画在画布上的位图

本文介绍了onTouchListener"通过&QUOT画在画布上的位图;在特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用画布通过触摸屏来绘制位图,但他们不显示在应用程序位图。

 公共类MainActivity延伸活动{
    公众的LinearLayout屏幕布置;    公共无效画(INT X,int y)对{
        位图B = BitmapFactory.de codeResource(getResources(),R.drawable.dam);
        帆布帆布=新的Canvas(B);
        canvas.drawBitmap(B,X,Y,NULL);
    }    公共无效的onCreate(捆绑saveInstanceState){
       super.onCreate(saveInstanceState);
       的setContentView(R.layout.activity_main);       最终的LinearLayout屏幕布置=(的LinearLayout)findViewById(R.id.screenlayout);
        screenlayout.setOnTouchListener(新OnTouchListener(){
                公共布尔onTouch(视图V,MotionEvent事件){
                    开关(event.getAction()){
                    案例MotionEvent.ACTION_DOWN:
                        INT X =(int)的event.getRawX();
                        INT Y =(int)的event.getRawY();                        绘制(X,Y);                        打破;
                    案例MotionEvent.ACTION_MOVE:
                        打破;
                    默认:
                        打破;
                    }
                    返回true;
                   }
            });
    }
 }


解决方案

试试这个,

在您的main.xml中。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
< / LinearLayout中>

在您的MainActivity类别。

 进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.View.OnTouchListener;
进口android.view.ViewGroup.LayoutParams;公共类DrawFunny扩展活动实现OnTouchListener {
    私人浮动X;
    私人浮动Ÿ;
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        MyCustomPanel视图=新MyCustomPanel(本);        ViewGroup.LayoutParams PARAMS =
                            新ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                                                       LayoutParams.FILL_PARENT);
        addContentView(视图,则params);
        view.setOnTouchListener(本);    }
    私有类MyCustomPanel扩展视图{        公共MyCustomPanel(上下文的背景下){
            超级(上下文);        }
        @覆盖
        公共无效画(油画画布){            涂料粉刷=新的油漆();
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(6);            canvas.drawLine(10,10,50,50,油漆);
            paint.setColor(Color.RED);            canvas.drawLine(50,50,90,10,漆);
            canvas.drawCircle(50,50,3,油漆);            canvas.drawCircle(X,Y,3,油漆);        }
    }
    公共布尔onTouch(视图V,MotionEvent事件){
        X = event.getX();
        Y = event.getY();        v.invalidate();
        返回true;
    }
}

I use Canvas to draw bitmap by touch screen but they don't show bitmap on apps.

public class MainActivity extends Activity {
    public LinearLayout screenlayout;

    public void draw (int x, int y){
        Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.dam);
        Canvas canvas=new Canvas(b);
        canvas.drawBitmap(b, x, y, null);
    }

    public void onCreate(Bundle saveInstanceState){
       super.onCreate(saveInstanceState);
       setContentView(R.layout.activity_main);

       final LinearLayout screenlayout= (LinearLayout)findViewById(R.id.screenlayout);
        screenlayout.setOnTouchListener(new OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        int x = (int) event.getRawX();
                        int y = (int) event.getRawY();

                        draw(x,y);

                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    default:
                        break;
                    }
                    return true;
                   }
            });
    }
 }
解决方案

try this,

in your main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</LinearLayout>

in your MainActivity Class.

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;

public class DrawFunny extends Activity implements OnTouchListener  {
    private float x;
    private float y;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyCustomPanel view = new MyCustomPanel(this);

        ViewGroup.LayoutParams params =
                            new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                                                       LayoutParams.FILL_PARENT);
        addContentView(view, params);
        view.setOnTouchListener(this);

    }
    private class MyCustomPanel extends View {

        public MyCustomPanel(Context context) {
            super(context);

        }
        @Override
        public void draw(Canvas canvas) {

            Paint paint = new Paint();
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(6);

            canvas.drawLine(10,10,50,50,paint);
            paint.setColor(Color.RED);

            canvas.drawLine(50, 50, 90, 10, paint);
            canvas.drawCircle(50, 50, 3, paint);

            canvas.drawCircle(x,y,3,paint);

        }
    }
    public boolean onTouch(View v, MotionEvent event) {
        x = event.getX();
        y = event.getY();

        v.invalidate();
        return true;
    }
}

这篇关于onTouchListener&QUOT;通过&QUOT画在画布上的位图;在特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:03