小刚张嘎
2018-01-29 09:28
采纳率: 85.7%
浏览 1.8k
已采纳

怎么在自定义ImageView上Draw东西,然后获取为Bitmap?

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view= View.inflate(this,R.layout.activity_main,null);
    //布局参数
    ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    ImageView imageView=new ImageView(this);
    //设置布局参数
    imageView.setLayoutParams(layoutParams);
    imageView.setBackgroundColor(Color.RED);

    //把textview加入RelativeLayout中
    ((RelativeLayout)view).addView(imageView);

    //显示整个布局
    setContentView(view);
    setContentView(new SmileyView(this));




}

}

class SmileyView extends View {
private Paint mCirclePaint;
private float mCenterX;
private float mCenterY;
private float mRadius;

 public SmileyView(Context context) {
    this(context, null);
}

public SmileyView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public SmileyView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initPaints();
}

private void initPaints() {
    mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mCirclePaint.setStyle(Paint.Style.FILL);
    mCirclePaint.setColor(Color.YELLOW);
}

 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     int w = MeasureSpec.getSize(widthMeasureSpec);
     int h = MeasureSpec.getSize(heightMeasureSpec);

     int size = Math.min(w, h);
     setMeasuredDimension(size, size);
 }

 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
     mCenterX = w / 2f;
     mCenterY = h / 2f;
     mRadius = Math.min(w, h) / 2f;
 }

protected void onDraw(Canvas canvas) {
    canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);
    // draw eyes


}

}

上面这个代码如何修改为在ImageView上显示画的圆,然后把ImageView转化成Bitmap?麻烦具体点,我小白

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • brave_shine 2018-01-31 08:22
    已采纳

    之前写过一个类似的,不过没自定义控件,在画布上画的文字,再把控件内容覆盖到上面,跟你这反过来就行了,你可以参考一下:

    //截取scrollview的屏幕
    public static Bitmap getScrollViewBitmap(NestedScrollView scrollView) {
            int h = 0;
            Bitmap bitmap;
            for (int i = 0; i < scrollView.getChildCount(); i++) {
                    h += scrollView.getChildAt(i).getHeight();
            }
            // 创建相应大小的bitmap
            bitmap = Bitmap.createBitmap(scrollView.getMeasuredWidth(), h,
                            Bitmap.Config.ARGB_4444);
            final Canvas canvas = new Canvas(bitmap);
            canvas.drawColor(Color.parseColor("#FFFFFF"));
            drawTextToBitmap(scrollView.getContext(), canvas, scrollView.getMeasuredWidth(), h);
            scrollView.draw(canvas);
            return bitmap;
    }
    
    // 给图片添加水印
    public static void drawTextToBitmap(Context context, Canvas canvas, int width, int height) {
            String logo = "戚大爷";
            Paint paint = new Paint();
            paint.setColor(SkinManager.getInstance().getResourceManager().getColor("future_text_bg"));
            paint.setAlpha(80);
            paint.setAntiAlias(true);
            paint.setTextSize((float) DensityUtil.dip2px(context, 30));
            canvas.save();
            canvas.rotate(-30);
            float textWidth = paint.measureText(logo);
            int index = 0;
            for (int positionY = -DensityUtil.dip2px(context, 30); positionY <= height; positionY += DensityUtil.dip2px(context, 80)) {
                    float fromX = -0.75f * height + (index++ % 2) * textWidth;
                    for (float positionX = fromX; positionX < width; positionX += textWidth * 2) {
                            canvas.drawText(logo, positionX, positionY, paint);
                    }
            }
            canvas.restore();
    }
    

    希望能够帮助到你!

    点赞 打赏 评论
  • 白萝卜blb 2018-01-29 09:59

    一般都是 在自定义View上面画东西,然后保存成bitmap

    //generate picture
    private Bitmap convertViewToBitmap(View view){
        Bitmap bitmap = Bitmap.createBitmap(view.getLayoutParams().width, view.getLayoutParams().height,
                Bitmap.Config.RGB_565);
    
        Log.i("blb", "-------convert view's height, :" + view.getLayoutParams().height + ", width: " + view.getLayoutParams().width + ", getHeight:" + view.getHeight());
        //利用bitmap生成画布
        Canvas canvas = new Canvas(bitmap);
    
        //把view中的内容绘制在画布上
        view.draw(canvas);
    
        return bitmap;
    }
    
        然后这样就生成bitmap了。
    
    点赞 打赏 评论
  • 小刚张嘎 2018-01-30 09:29

    还是不知道如何获取到画出来的界面。

    点赞 打赏 评论
  • 熊华贵 2018-01-30 10:36

    public static Bitmap convertViewToBitmap(View view){
    view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();

        return bitmap;
    }
    
    点赞 打赏 评论
  • 小刚张嘎 2018-01-31 06:20

    public class MainActivity extends AppCompatActivity {
    ImageView imageView; Bitmap bitmap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    imageView=(ImageView) this.findViewById(R.id.imageView);
    View view= View.inflate(this,R.layout.activity_main,null);

        //布局参数
        ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
    
        setContentView(new SmileyView(this,null));
        bitmap= convertViewToBitmap(view);
    
        if(bitmap!=null){
            Log.d("xg","not null");
           //imageView.setImageBitmap(bitmap);
    
        }else {
            Log.d("xg","null,null, null");
        }
        //调用保存方法
        saveBitmap();
    }
    //吧view转化为bitmap
    public static Bitmap convertViewToBitmap(View view){
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        Log.i("xg",view.getMeasuredWidth()+"--"+view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        view.destroyDrawingCache();
        return bitmap;
    }
    
    
    public void saveBitmap() {
    
    
        String state= Environment.getExternalStorageState();
        File file;
    
        try {
            if(Environment.MEDIA_MOUNTED.equals(state)){
                // 保存图片到SD卡上
                file = Environment.getExternalStorageDirectory();
            }else{
                file =getFilesDir();
            }
    
            FileOutputStream stream = new FileOutputStream(file.getAbsolutePath()+"/sign/"+System.currentTimeMillis()+".png");
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            Toast.makeText(MainActivity.this, "保存图片成功", 0).show();
    
    
            stream.flush();
            stream.close();
    
        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "保存图片失败", 1).show();
            e.printStackTrace();
        }
    
    
    }
    

    }

    class SmileyView extends View {
    private Paint mCirclePaint;
    private float mCenterX;
    private float mCenterY;
    private float mRadius;

    public SmileyView(Context context) {
        this(context, null);
    }
    
    public SmileyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    
    public SmileyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaints();
    }
    
    private void initPaints() {
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStyle(Paint.Style.FILL);
        mCirclePaint.setColor(Color.YELLOW);
    }
    
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = MeasureSpec.getSize(widthMeasureSpec);
        int h = MeasureSpec.getSize(heightMeasureSpec);
    
        int size = Math.min(w, h);
        setMeasuredDimension(size, size);
    }
    
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCenterX = w / 2f;
        mCenterY = h / 2f;
        mRadius = Math.min(w, h) / 2f;
    }
    
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(mCenterX, mCenterY, mRadius, mCirclePaint);
        // draw eyes
    
    
    }
    

    }
    bitmap都已经不为空了,还是无法保存,保存代码试过的,也没问题,读写权限,包括手机上的储存权限也是开的。

    点赞 打赏 评论

相关推荐 更多相似问题