2017-09-16 95 views
0

我在相對佈局中添加了自定義視圖,並且想要包含曲線文本的相對佈局的位圖,並且希望在imageview中存儲該位圖。如何將包含曲線文本的自定義視圖轉換爲位圖?

' RelativeLayout relativeLayout;

private 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

試試這個。

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; 
} 
相關問題