截图并在上面绘图(android)

时间:2016-12-16 11:09:19

标签: android android-intent bitmap drawing screenshot

我尝试截取屏幕截图并将其意图发送给另一个活动。 在另一个活动中,我有一个带有自定义视图的类,它提供了绘图。 我想从另一个活动中截取屏幕截图。再次截取屏幕截图并保存在我的SD卡上。

所以,在我的第一个活动中,我会截取屏幕截图并将其发送到另一个活动:

  private void takeScreenshot() {

    try {
        // create bitmap screen capture
        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setClass(MainActivity.this, CaptureSignature.class);
        intent.putExtra("SCREENSHOT", bitmap);
        startActivity(intent);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

有关视图的活动

 public class CaptureSignature extends AppCompatActivity {
    Signature mSignature;
    Paint paint;
    ImageView mContent;
    Button clear, save;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capturesignature);

    save = (Button) findViewById(R.id.save);
    save.setEnabled(false);
    clear = (Button) findViewById(R.id.clear);
    mContent = (ImageView) findViewById(R.id.mysignature);



    Bitmap bitmap = this.getIntent().getParcelableExtra("SCREENSHOT");
    mContent.setImageBitmap(bitmap);

    mSignature = new Signature(this, null,bitmap);

    save.setOnClickListener(onButtonClick);
    clear.setOnClickListener(onButtonClick);
}

Button.OnClickListener onButtonClick = new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == clear) {
            mSignature.clear();
        } else if (v == save) {
            mSignature.save();
        }
    }
};

public class Signature extends View {
    static final float STROKE_WIDTH = 10f;
    static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;
    Paint paint = new Paint();
    Path path = new Path();
    private Bitmap mBackground;

    float lastTouchX;
    float lastTouchY;
    final RectF dirtyRect = new RectF();

    public Signature(Context context, AttributeSet attrs, Bitmap background) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
        this.mBackground = background;
    }

    public void clear() {
        path.reset();
        invalidate();
        save.setEnabled(false);
    }

    public void save() {
        Bitmap returnedBitmap = Bitmap.createBitmap(mContent.getWidth(),
                mContent.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = mContent.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        mContent.draw(canvas);

        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        returnedBitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
        Intent intent = new Intent();
        intent.putExtra("byteArray", bs.toByteArray());
        setResult(1, intent);
        finish();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        canvas.drawBitmap(mBackground,0f,0f,null);
        canvas.drawPath(path, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        save.setEnabled(true);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                lastTouchX = eventX;
                lastTouchY = eventY;
                return true;

            case MotionEvent.ACTION_MOVE:

            case MotionEvent.ACTION_UP:

                resetDirtyRect(eventX, eventY);
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++) {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    path.lineTo(historicalX, historicalY);
                }
                path.lineTo(eventX, eventY);
                break;
        }

        invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH),
                (int) (dirtyRect.top - HALF_STROKE_WIDTH),
                (int) (dirtyRect.right + HALF_STROKE_WIDTH),
                (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
    }

    private void resetDirtyRect(float eventX, float eventY) {
        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
    }
}

}

那么,我有什么问题: 1)位图太大,不能按意图传输,所以我无法实现这一点 2)它是否正确解决方案?

也许,任何人都知道,如何实现这项任务

0 个答案:

没有答案