2017-09-16 7 views
0

J'ai ajouté une vue personnalisée dans une disposition relative et je veux un bitmap de cette disposition relative contenant le texte de la courbe et je veux stocker cette image dans l'image.Comment convertir une vue personnalisée contenant du texte courbe en bitmap?

' RelativeLayout relativeLayout;

privé 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); 
    } 
}`' 

Répondre

1

Essayez ceci.

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; 
}