如何将包含曲线文本的自定义视图转换为位图?

时间:2017-09-16 16:32:12

标签: android-bitmap

我在相对布局中添加了自定义视图,并希望包含曲线文本的相对布局的位图,并希望将该位图存储在imageview中。

” RelativeLayout relativeLayout;

私人ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    relativeLayout= (RelativeLayout) findViewById(R.id.rel);

    imageView= (ImageView) findViewById(R.id.img1);


    Circle circle=new Circle(MainActivity.this);

    relativeLayout.addView(circle);






}



public class Circle extends View {
    Paint paint = new Paint();
    Path path = new Path();
    private static final String s = "Hello world example";

    public Circle(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        paint.setAntiAlias(true);
        paint.setTextSize(30);
    }

    public void onDraw(Canvas c) {
        c.rotate(180, getWidth()/2, getHeight()/2);
        path.addCircle(getWidth()/2, getHeight()/2, 90, Path.Direction.CW);
        c.drawTextOnPath(s, path, 0, 10, paint);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }
}`'

1 个答案:

答案 0 :(得分:1)

试试这个。

    Bitmap bitmap = loadBitmapFromView(this, relImagePreviewParent);    mPath = Environment.getExternalStorageDirectory() + File.separator + "screen_" + System.currentTimeMillis() + ".JPEG";

    File imageFile = new File(mPath);
    OutputStream fout = null;
    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, fout);
        fout.flush();
        fout.close();
        String tempPath = Environment.getExternalStorageDirectory() + File.separator + "screen_" + System.currentTimeMillis() + ".JPEG";
        File compressPath = new File(tempPath);


        Bitmap tempbitmap = GIFUtils.compressImage(imageFile.getAbsolutePath(), ImageEditingActivity.this, bitmap);
        OutputStream fileOut = new FileOutputStream(compressPath);
        tempbitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
        fileOut.flush();
        fileOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


   public static Bitmap loadBitmapFromView(Context context, View v) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(returnedBitmap);
    v.draw(c);

    return returnedBitmap;
}