android - 使用 Android 在 Canvas 上添加图像

标签 android android-layout android-intent android-widget

我是 android java 的新手,经过长时间的在线研究,我真的不知道如何让按钮出现在我的 VIEW 上。当我调试它时,我可以在布局中看到我的按钮,但在我的 View 中看不到。我不确定如何在单击按钮后放入多个图像。如果我在代码中做错了,请纠正我。 任何帮助将不胜感激。谢谢!

我的代码如下:

package com.example.oncanvas;

import android.app.Activity;

public class Main extends Activity {

    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DrawView DrawView = new DrawView(this);
        DrawView.setBackgroundColor(Color.WHITE);
        setContentView(DrawView);
        DrawView.requestFocus();

}

    public class DrawView extends View {

        private Bitmap  mBitmap;
        private Canvas  mCanvas;
        private Path    mPath;
        private Paint   mBitmapPaint;
        private Paint   mPaint;

        public DrawView(Context c) {
            super(c);

            mPath = new Path();
            mBitmapPaint = new Paint(Paint.DITHER_FLAG);


            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(0xFF000000);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(3);
        }  

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }

    public void clear(){
        mBitmap.eraseColor(Color.TRANSPARENT);
        invalidate();
        System.gc();
    }}

这是用于布局的 XML

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

<Button 
    android:text="Button" 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">

</Button>

<com.example.oncanvas
    android:id="@+id/drawView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">        

        </com.example.oncanvas>
</LinearLayout>

最佳答案

这只是一个线索..添加你的代码

  @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
        super.draw(canvas, mapView, shadow);                  

        // convert point to pixels
        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);

        // add marker
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
        canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 24, null); // 24 is the height of image       
        return true;
    }
} 

关于android - 使用 Android 在 Canvas 上添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13832713/

相关文章:

java - 等待一个异步操作完成android

java - Android 应用程序中的 Google 登录

Android:当acitvity支持横向和纵向时如何设置默认方向

android - 为应用程序创建 Android Assets (LPDI、MDPI、HDPI、XHDPI)?

android - Activity 识别 API 无法连续工作

android - 在将文本共享到应用程序后*仅*显示 toast (无 UI)?

android - 从后台线程查询房间数据库

java - Jackson SAX 解析器在解析巨大的 JSON 时抛出异常

android - 一个屏幕上的两个 RecyclerView

java - 手机重启后短信广播接收器不工作