小刚张嘎 2018-01-29 09:28 采纳率: 100%
浏览 1888
已采纳

怎么在自定义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();
    }
    

    希望能够帮助到你!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 关于php页面调取、更新数据提交刷新等问题
  • ¥50 使用 java+selenium 接管手动打开的IE浏览器
  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab