14

我是这里的新用户,并且正在使用请求自定义滚动视图的 android 应用程序(如下面的链接所示)。除了第一个图像外,它与网格视图非常相似。我尝试使用添加一个大的图像视图和网格视图。但它失败了。有人有什么建议吗?

在此处输入图像描述

4

1 回答 1

9

我设法使用以下代码获得以下图像: 在此处输入图像描述

我将代码移至此博客:

// please check this part.
            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {
                ImageView imageView;
                if(arg1==null){
                    imageView = new ImageView(DemoGridViewActivity.this){
                        @Override
                        protected void onMeasure(int widthMeasureSpec,
                                int heightMeasureSpec) {
                            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                            setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
                        }
                    };
                }else{
                    imageView = (ImageView) arg1;
                }

                imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
                imageView.setBackgroundColor(Color.BLUE);
                imageView.setScaleType(ScaleType.FIT_XY);
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//according to the position return proper imageview with bitmap
//for case 0 - top-left part
//for case 1 - top-right
//for case 5 - bottom-left
//for case 6 - bottom-right


                switch(arg0){
                case 0:
                    imageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth()/2, bitmap.getHeight()/2));
                    imageView.setBackgroundColor(Color.RED);
                    return imageView;
                case 1:
                    imageView.setImageBitmap(Bitmap.createBitmap(bitmap, bitmap.getWidth()/2, 0, bitmap.getWidth()/2, bitmap.getHeight()/2));
                    imageView.setBackgroundColor(Color.GREEN);
                    return imageView;
                case 5:
                    imageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, bitmap.getHeight()/2, bitmap.getWidth()/2, bitmap.getHeight()/2));
                    imageView.setBackgroundColor(Color.YELLOW);
                    return imageView;
                case 6:
                    imageView.setImageBitmap(Bitmap.createBitmap(bitmap, bitmap.getWidth()/2, bitmap.getHeight()/2, bitmap.getWidth()/2, bitmap.getHeight()/2));
                    imageView.setBackgroundColor(Color.MAGENTA);
                    return imageView;
                default:
                    imageView.setImageResource(R.drawable.ic_launcher);
                    return imageView;
                }
            }

        }
    }


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:numColumns="5" >
        </GridView>

</LinearLayout>
于 2012-06-20T03:09:15.953 回答