【问题标题】:I want to save my gridview in one imageview我想将我的网格视图保存在一个图像视图中
【发布时间】:2014-03-08 19:07:54
【问题描述】:

如何在一个图像视图中保存我的网格完整网格视图项目有人帮助我。在下面的代码中,这是我用于网格视图的 ImageAdapter,这是我的 gridview 项目,我想在图像视图的 sdcard 中保存“mthum”位图数组

            public  class ImageAdapter extends BaseAdapter {
            private Context mContext;
            Bitmap bit = Bitmap.createScaledBitmap(S.Murge_Bitmap, 280,  280, false);
            public Bitmap[] mThum={bit,bit,bit,bit,bit,bit,bit,bit,bit,bit,bit,bit};
            public ImageAdapter(Context c) {
            mContext = c;
            }

            public int getCount() {
            return mThum.length;
            }

            public Object getItem(int position) {
            return mThum[position];
            }
            public long getItemId(int position) {
            return 0;
            }


            public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {   
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setPadding(1, 1, 1, 1);
            } else {
            imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(mThum[position]);
            return imageView;
            } 

}

【问题讨论】:

    标签: android gridview filesystems android-sdcard


    【解决方案1】:

    你可以使用这个解决方案,但我认为它只会给你一个显示图像的位图:

    Bitmap b = Bitmap.createBitmap(mGridView.getDrawingCache());
    

    如果您对此有疑问并且无法获取位图,请尝试以下操作:

    mGridView.setDrawingCacheEnabled(true); 
    // Without it the view will have a dimension of 0,0 and the bitmap will be null          
    mGridView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    mGridView.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
    
    mGridView.buildDrawingCache(true);
    Bitmap b = Bitmap.createBitmap(mGridView.getDrawingCache());
    mGridView.setDrawingCacheEnabled(false); // clear drawing cache
    

    否则,如果您想要“适配器”中的所有图像,您可以像此答案中描述的那样构建您的位图:

    https://stackoverflow.com/a/15152221/1686472

    编辑:我没有看到您想将图像保存在 SD 卡上,对此有很多答案,所以您应该看看它们:

    https://stackoverflow.com/a/15662384/1686472

    【讨论】:

    • 这正在运行我在我的活动中使用了这个代码谢谢
    • 请将其标记为已回答。检查我答案左上角的标记。
    • 它在 Bitmap b = Bitmap.createBitmap(gridview.getDrawingCache()); 行给了我错误 NullPointerException
    • 你可以检查你的 gridview != null && gridview.getDrawingCache() != null 。但是你不会有你的位图。渲染gridview后需要调用该方法。
    • 感谢它的工作,但我想将整个 gridview 图像转换为一个位图,而不仅仅是可见部分。有可能吗?
    猜你喜欢
    • 2021-09-02
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多