从FrameLayout创建的位图是黑色

时间:2017-08-23 16:21:23

标签: android canvas bitmap android-framelayout

我正在尝试为共享intent.Bitmap创建一个位图(我希望因为它在尝试buildDrawingCache()解决方案时不是null)但是当它被共享时它是null。我已经查看了其他SO也回答,但没有一个有效。在我的代码中你可以看到我也尝试了其他解决方案但是创建的位图在那里是空的。至少创建了位图,但它是黑色的。如果重要的话,我的图像是500px X 500px图像。

share_image.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/msg_bg"/>
        <TextView
            android:id="@+id/msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:padding="35dp"
            android:textColor="@android:color/black"
            android:layout_gravity="center_vertical|center_horizontal"/>
    </FrameLayout>
</LinearLayout>

这是我尝试创建位图的地方,后来一个是将位图压缩为PNG并调用共享意图的函数。

    View image=inflater.inflate(R.layout.share_image,null,false);
                                        FrameLayout frame=(FrameLayout)image.findViewById(R.id.frame);
                                        TextView text=(TextView)image.findViewById(R.id.msg);
                                        text.setText(msg.getMessage());

//                                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(500, 500);
//                                        frame.setLayoutParams(layoutParams);
//                                        frame.setDrawingCacheEnabled(true);
//                                       frame.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                                                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//                                        frame.layout(0, 0, frame.getMeasuredWidth(), frame.getMeasuredHeight());
//                                        frame.buildDrawingCache();
//                                        Bitmap bm=frame.getDrawingCache();
//                                        frame.destroyDrawingCache();

                                        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(500, 500);
                                        frame.setLayoutParams(layoutParams);
                                        Bitmap b = Bitmap.createBitmap( frame.getLayoutParams().width, frame.getLayoutParams().height, Bitmap.Config.ARGB_8888);
                                        Bitmap finalBitmap=b.copy(Bitmap.Config.ARGB_8888, true);
                                        Canvas c = new Canvas(finalBitmap);
                                        frame.layout(0, 0, frame.getLayoutParams().width, frame.getLayoutParams().height);
                                        frame.draw(c);

//                                        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.msg_bg);
//                                        Bitmap finalBitmap=bitmap.copy(Bitmap.Config.ARGB_8888, true);
//                                        Canvas canvas = new Canvas(finalBitmap);
//                                        Paint paint = new Paint();
//                                        paint.setColor(Color.BLACK);
//                                        paint.setTextSize(48f);
//                                        Rect bounds = new Rect();
//                                        paint.getTextBounds(msg.getMessage(), 0, msg.getMessage().length(), bounds);
//                                        int x = (finalBitmap.getWidth() - bounds.width())/2;
//                                        int y = (finalBitmap.getHeight() + bounds.height())/2;
//                                        paint.setTextSize(48f*500/bounds.width());
//                                        canvas.drawText(msg.getMessage(), x, y, paint);
                                        shareBitmap(finalBitmap);



private void shareBitmap (Bitmap bitmap) {
        try {
            File file = new File(getActivity().getCacheDir(),"pk.png");
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
            file.setReadable(true, false);
            final Intent intent = new Intent(     android.content.Intent.ACTION_SEND);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            intent.setType("image/png");
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

0 个答案:

没有答案