I am new to android and I am trying to do 1 small project in it..
I am trying to get a picture from the drawable folder and cutting it into 9 pieces and storing those pieces in a bitmap array..I was able to do that..
Now, how do i retrieve those pieces from the bitmap array and display it on the imageview?
I have used a gridview in the UI to show the images…. but am not able to assign the bitmap array to the imageResource…
ok, here is what i am trying to do..
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
Activity activity=(Activity)mContext;
Resources res=activity.getResources();
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap img=BitmapFactory.decodeResource(res, R.drawable.kite);
createBitmapPieces(img);
imageView.setImageResource(bmp[2]);//I dont know how to set the image from the array
}
public Bitmap[] createBitmapPieces(Bitmap source){
int k=0;
int width=source.getWidth();
int height=source.getHeight();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
bmp[k]=Bitmap.createBitmap(source,(width*j)/3,(i*height)/3,width/3,height/3);
k++;
}
}
return bmp;
}
You can assign only one Bitmap to an
ImageView. If you want to use an array, you have to extend theImageViewclass, and handle the array yourself in your customView.Two things you have to do:
1.) You have to create a public method for setting the
Bitmaparray to a new private field.2.) You have to overwrite the
onDraw(Canvas canvas)method and draw theBitmapsin the array instead of drawing a singleDrawable.EDIT: I might have misunderstood your question, if you try to add the
Bitmapsto theGridView, instead of a singleImageView, then you can do it with a custom adapter:And in your
Activity: