Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adding More Images
- Loading branch information
1 parent
abf3850
commit de6fe4f
Showing
12 changed files
with
472 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions
114
app/src/main/java/dragosholban/com/androidpuzzlegame/ImageAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package dragosholban.com.androidpuzzlegame; | ||
|
||
import android.content.Context; | ||
import android.content.res.AssetManager; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.graphics.Rect; | ||
import android.os.AsyncTask; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.ImageView; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class ImageAdapter extends BaseAdapter { | ||
private Context mContext; | ||
private AssetManager am; | ||
private String[] files; | ||
|
||
public ImageAdapter(Context c) { | ||
mContext = c; | ||
am = mContext.getAssets(); | ||
try { | ||
files = am.list("img"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public int getCount() { | ||
return files.length; | ||
} | ||
|
||
public Object getItem(int position) { | ||
return null; | ||
} | ||
|
||
public long getItemId(int position) { | ||
return 0; | ||
} | ||
|
||
// create a new ImageView for each item referenced by the Adapter | ||
public View getView(final int position, View convertView, ViewGroup parent) { | ||
if (convertView == null) { | ||
final LayoutInflater layoutInflater = LayoutInflater.from(mContext); | ||
convertView = layoutInflater.inflate(R.layout.grid_element, null); | ||
} | ||
|
||
final ImageView imageView = convertView.findViewById(R.id.gridImageview); | ||
imageView.setImageBitmap(null); | ||
// run image related code after the view was laid out | ||
imageView.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
new AsyncTask<Void, Void, Void>() { | ||
private Bitmap bitmap; | ||
@Override | ||
protected Void doInBackground(Void... voids) { | ||
bitmap = getPicFromAsset(imageView, files[position]); | ||
return null; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Void aVoid) { | ||
super.onPostExecute(aVoid); | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
}.execute(); | ||
} | ||
}); | ||
|
||
return convertView; | ||
} | ||
|
||
private Bitmap getPicFromAsset(ImageView imageView, String assetName) { | ||
// Get the dimensions of the View | ||
int targetW = imageView.getWidth(); | ||
int targetH = imageView.getHeight(); | ||
|
||
if(targetW == 0 || targetH == 0) { | ||
// view has no dimensions set | ||
return null; | ||
} | ||
|
||
try { | ||
InputStream is = am.open("img/" + assetName); | ||
// Get the dimensions of the bitmap | ||
BitmapFactory.Options bmOptions = new BitmapFactory.Options(); | ||
bmOptions.inJustDecodeBounds = true; | ||
BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions); | ||
int photoW = bmOptions.outWidth; | ||
int photoH = bmOptions.outHeight; | ||
|
||
// Determine how much to scale down the image | ||
int scaleFactor = Math.min(photoW/targetW, photoH/targetH); | ||
|
||
is.reset(); | ||
|
||
// Decode the image file into a Bitmap sized to fill the View | ||
bmOptions.inJustDecodeBounds = false; | ||
bmOptions.inSampleSize = scaleFactor; | ||
bmOptions.inPurgeable = true; | ||
|
||
return BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.