Reputation: 2927
i have TextView
content Images from remote server and draw them into TextView
, i want to make this downloaded or draw image clickable on click open image in large mode for example
how to make Drawable
clickable or has onclicklistener
?
this is my class i used for Html Form
for example if TextView
has <img src="http://www.bla.com/1.jpg" />
after class download and draw this image how i can make it has on click listener to open it in large mode !
`public class URLImageParser implements ImageGetter {
//Activity Context
Context c;
//TextView Content
TextView container;
//File Cache
FileCache fileCache;
//Imgae Resulotion
int IMGAE_REZ = 100;
/*************************************************
* Setup Class Values
*/
public URLImageParser( Context c,int position,View t) {
this.c = c;
this.container = (TextView) t;
fileCache = new FileCache(c);
}
/*************************************************
* Start Draw
*/
@SuppressWarnings("deprecation")
public Drawable getDrawable(String source) {
//Fix Url Spaces
source = ( source != null ) ? source.replace(" ", "%20") : null;
//Not cached yet Fetch and Draw
if ( source != null )
{
//Check if Cached by Bitmap
Bitmap cached = Api.bitmap_cache.get(source);
//if not in Bitmap Cache get from file cache or Download it
if ( cached != null )
{
Drawable drw;
drw = new BitmapDrawable(cached);
drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));
return drw;
}else
{
URLDrawable urlDrawable = new URLDrawable();
// get the actual source
ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable , source);
asyncTask.execute(source);
return urlDrawable;
}
}else
{
return null;
}
}
/*******************************************************
* Decode file as Bitmap
* Return Bitmap Decoded
*/
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<IMGAE_REZ || height_tmp/2<IMGAE_REZ)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
/*******************************************************
* AsyncTask Download or get from cache Drawable
* Setting Image if exists or broken image
* Below this class nothing related to Above
*/
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
//Image Draw
URLDrawable urlDrawable;
//If called from Cache BITMAP
boolean is_cached = false;
//File Cache id in BitmapCache
String cacheID;
//Bitmap to Cache
Bitmap cacheMe;
public ImageGetterAsyncTask(URLDrawable d, String cacheID) {
this.urlDrawable = d;
this.cacheID = cacheID;
if ( d == null )
{
cancel(true);
}
}
/*****************************************************
* Get Draw
*/
@Override
protected Drawable doInBackground(String... params) {
return fetchDrawable(params[0]);
}
/*****************************************************
* Draw Image and Set Bounds if not Set
*/
@Override
protected void onPostExecute(Drawable result) {
//if Drawable not null procced
if ( result != null )
{
urlDrawable.setBounds(0,0, (int)(result.getIntrinsicWidth()*5.5),(int)(result.getIntrinsicHeight()*5.5));
urlDrawable.drawable = result;
int newhight = (URLImageParser.this.container.getHeight() + (int)(result.getIntrinsicHeight()*5.5));
URLImageParser.this.container.setHeight(newhight);
URLImageParser.this.container.setEllipsize(null);
URLImageParser.this.container.requestLayout();
URLImageParser.this.container.invalidate();
Api.bitmap_cache.put(cacheID, cacheMe);
}
}
/*****************************************************
* Fetch Draw convert from Bitmap to Draw
* Return Draw
*/
@SuppressWarnings("deprecation")
public Drawable fetchDrawable(String urlString) {
Drawable drw = null;
cacheMe = downloadFile(urlString);
if ( cacheMe != null )
{
drw = new BitmapDrawable(cacheMe);
drw.setBounds(0,0, (int)(drw.getIntrinsicWidth()*5.5),(int)(drw.getIntrinsicHeight()*5.5));
}
return drw;
}
/*******************************************************
* Return Bitmap of Image String if downloaded or Cached
* Return Bitmap
*/
private Bitmap downloadFile (String url )
{
Bitmap bitmap = null;
File f = fileCache.getFile(url);
bitmap = decodeFile(f);
//return from cached file
if ( bitmap != null )
{
return bitmap;
}else
{
try {
//Download and Cache file
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is=conn.getInputStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
} catch (Throwable ex){
ex.printStackTrace();
if(ex instanceof OutOfMemoryError)
{
Api.bitmap_cache.clear();
}
}
}
return bitmap;
}
}
}`
Upvotes: 0
Views: 987
Reputation: 6905
how to make Drawable clickable or has onclicklistener ?
You don't need a listner for the Drawable
image, but for your TextView
.
in your XML
: android:clickable="true"
and onClick="your_method"
in your Java
: textview.setClickable(true)
and textView.setOnClickListner(your_listner)
Upvotes: 1