touch event on image switcher - android

i have this code with me and it is running successfully but i want to add touch event on it for changing of image and also for zoom in and zoom out effect....
this is my code:
package com.conn;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;
public class image_slider extends Activity implements ViewFactory {
Integer pics[] = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d,
R.drawable.e };
ImageSwitcher iSwitcher;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
iSwitcher.setFactory(this);
iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery gallery = (Gallery) findViewById(R.id.Gallery01);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
iSwitcher.setImageResource(pics[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
public ImageAdapter(Context c) {
ctx = c;
}
#Override
public int getCount() {
return pics.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iView = new ImageView(ctx);
iView.setImageResource(pics[arg0]);
iView.setScaleType(ImageView.ScaleType.FIT_XY);
iView.setLayoutParams(new Gallery.LayoutParams(150, 150));
return iView;
}
}
#Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new
ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
}

Go through links below it has the all features which you required.
PinchZooming
http://code.google.com/p/android-pinch/wiki/PinchImageView
http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/
One finger Zooming
http://blogs.sonyericsson.com/wp/2010/05/18/android-one-finger-zoom-tutorial-part-1/
Hope this helps.
I have modified the class as per your needs.. use this PinchImageView instead of ImageView..so simple
Check this out here ...http://pastebin.com/eRH2D2nF
Thanks..
Giri

Related

performItemClick doesn't start animation

I'm creating a ListView with many item, each row of ListView contains a TextView and a "Remove" button, when click on Remove button (not click on the row), that row should show the toast, run the animation and remove from the list but it only show the toast (It works well when I click on the row)
This is my code
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String[] items = new String[20];
ListView lv;
MyAdapter mAdapter;
ArrayList<String> data = new ArrayList<String>();
private int position;
Animation ani;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 20; i++) {
items[i] = String.valueOf(i + 1);
data.add(items[i]);
}
lv = (ListView) findViewById(R.id.listView1);
mAdapter = new MyAdapter(getApplication(), R.layout.custom_row, data);
lv.setAdapter(mAdapter);
ani = AnimationUtils.loadAnimation(getApplicationContext(), R.animator.fadeout);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "You have clicked on item " + arg2, Toast.LENGTH_SHORT).show();
arg1.startAnimation(ani);
ani.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
mAdapter.remove(arg2);
}
});
}
});
}
class MyAdapter extends ArrayAdapter<String> {
ArrayList<String> data;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(layout, parent, false);
TextView tv = (TextView) v.findViewById(R.id.textView1);
tv.setText(data.get(position));
Button bt = (Button) v.findViewById(R.id.button1);
bt.setOnClickListener(new MyClick(this, position));
return v;
}
class MyClick implements View.OnClickListener {
MyAdapter p;
int i;
public MyClick(MyAdapter parent, int pos) {
p = parent;
i = pos;
}
#Override
public void onClick(View v) {
lv.performItemClick(lv.getAdapter().getView(i, null, null), i, i);
}
}
public void remove(int pos) {
data.remove(pos);
notifyDataSetChanged();
}
int layout;
public MyAdapter(Context c, int l, ArrayList<String> data_name) {
super(c, l, data_name);
layout = l;
data = data_name;
}
}
}
Thank you in advance for your help.
Modify your onClick callback method in MyClick class like below:
#Override
public void onClick(View v) {
lv.performItemClick(lv.getChildAt(i), i, i);
}
Because p.getView(i, null, null) will inflate a new View that isn't the item in ListView, so you should use lv.getChildAt(i) to get the actual item.

Grid view to stay intact once imageview image is changed

I am developing a game in Android where in the alpha value is changed once the image in the imageview and the image in the grid view match. If the number of matches are equal to 4, I need to change the image in the imageview and again compare with the images in the grid view.
I change the image in the imageview using imageView.setImageResource(q);
The image gets changed but the grid view disappears. Could you please tell me what I need to do for the grid view to change intact?
I tried gridView.invalidateViews(); but in vain. I also created a new imageadapter with the same context but it still did not work. Any help would be really great!
package com.example.despicablemehunt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
final public Integer[] mThumbIds = {
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4
};
int i=0;
int p=0;
final Integer q=0;
public Context mContext;
List<Integer> imagesArrayList = new ArrayList<Integer>(Arrays.asList(mThumbIds));
Integer [] tempArrayList;
public static final Random rgenerator = new Random();
public static final Integer[] mImageIds =
{ R.drawable.icon1, R.drawable.icon2, R.drawable.icon3,R.drawable.icon4 };
#Override
protected void onCreate(Bundle savedInstanceState) {
final long time= System.currentTimeMillis();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Collections.shuffle(imagesArrayList);
tempArrayList = (Integer[])(imagesArrayList.toArray(new Integer[imagesArrayList.size()]));
final GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(q);
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressLint("NewApi")
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(getResources().getDrawable(q).getConstantState().equals(getResources().getDrawable(tempArrayList[arg2]).getConstantState()))
{
if(p==5)
{
/*final long time1= System.currentTimeMillis();
if ((time1-time)> 20)
{
}*/
Toast.makeText(getApplicationContext(), "DONE DONE DONE", Toast.LENGTH_SHORT).show();
}
arg1.setAlpha((float) 0.4);
if((++i)== 4)
{
gridView.setAlpha((float)0.0);
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
imageView.setImageResource(q);
i=0;
gridView.invalidate();
}
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter {
// Keep all Images in array
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(tempArrayList[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
}
To refresh the gridview you have to use gridView.invalidateViews(); So change it
after 4 correct click on gridview image why are you setting alpha 0 in gridview. So just remove the line
gridView.setAlpha((float)0.0);
//setting alpha causing the gridview invisible
so your code should be like this
if ((++i) == 4) {
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
iv.setImageResource(q);
i = 0;
gridView.invalidateViews();
}
hope this will help
Thanks

Having difficulty getting images from sdcard and displaying in gallery

I seem to have a problem loading images into my gallery from sdcard. I know I am doing something wrong with the code but don't know what. Can anyone please help me.
here is my GalleryActivity.java:
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.content.res.TypedArray;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
Cursor cursor;
String[] imageIDs = {MediaStore.Images.Thumbnails._ID};
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
{
};
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),"pic" + (position + 1) + "selected", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId (R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageIDs));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
The error is under this bit of code which is to retrieve the images from the cdcard (Is this the correct code for retrieving images from sdcard?)
String[] imageIDs = {MediaStore.Images.Thumbnails._ID};
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
Hope anyone can help me?
These code lines have go into a method. You have them written outside any method.
GalleryActivity(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imageIDs, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
cursor = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

Saving image to SD card not working

Having a problem at the bottom, I'm trying to create a case to save the image to SD card yet it isn't working and I don't know why.
I think it's got something to do with the getRawResource but still not sure.
Thanks for any help!!!
package com.nk_apps.random;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class Main extends Activity {
private Gallery gallery;
private ImageView imgView;
private Integer[] imgId = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView)findViewById(R.id.imageView01);
imgView.setImageResource(imgId[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(imgId[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return imgId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(imgId[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(170, 270));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bsaveImage:
InputStream inps = getResources().openRawResource(GalItemBg);
Bitmap bmp = BitmapFactory.decodeStream(inps);
try{
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "Title" , "Hip-Hop Wallpapers");
Toast saveToast = Toast.makeText(Main.this, "The image has been saved.", Toast.LENGTH_SHORT);
saveToast.show();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
} }
Did you verify you have write permission to sdcard ? (this is WRITE_EXTERNAL_STORAGE)
Also, you should make sure you look at the right mount point to find the image (can be "internal sdcard" for some devices, for example).

Android Fragment List and GridView

i try to make two fragments in honeycomb, list and gridview.When i clicked the list-item, the GridView successfully displayed.but,the list was disappear.GridView should be displayed beside the list.here's my code:
FragmentTestActivity.java
package com.android.tabgrid;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentTestActivity extends FragmentActivity implements
OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.list_icon);
ArrayAdapter<String> yoyo = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,new String[] {
"Traffic",
"Forum",
"Promo",
"Others"
});
lv.setAdapter(yoyo);
lv.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Fragment f = new TestFragment(position+1);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.the_frag,f);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
}
TestFragment.java
package com.android.tabgrid;
import com.android.content.GridView1;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class TestFragment extends Fragment {
private int magznumber;
public TestFragment() {
}
public TestFragment(int position) {
this.magznumber = position;
}
#Override
public void onCreate(Bundle saved) {
super.onCreate(saved);
if (null != saved) {
magznumber = saved.getInt("magznumber");
}
}
#Override
public void onSaveInstanceState(Bundle toSave) {
toSave.putInt("magznumber", magznumber);
}
/**
* Make a grid to view the magazines
*/
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT, 0);
l.setLayoutParams(params);
ImageView i = new ImageView(c);
switch(magznumber){
case 1:
Intent i1 = new Intent(getActivity(), GridView1.class);
startActivity(i1);
break;
case 2:
i.setImageResource(R.drawable.lfymag);
break;
case 3:
i.setImageResource(R.drawable.ffymag);
break;
}
l.addView(i);
return l;
}
}
GridView1.java
package com.android.content;
import com.android.tabgrid.FragmentTestActivity;
import com.android.tabgrid.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.android.tabgrid.TestFragment;
public class GridView1 extends FragmentActivity{
Integer[] img = {
R.drawable.btn_spot_u,
R.drawable.btn_traffic_u,
R.drawable.btn_forum_u,
R.drawable.btn_promo_u
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_1);
GridView gV = (GridView)findViewById(R.id.grid1);
gV.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
public ImageAdapter(Context c) {
ctx = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return img.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imgView;
if(v == null) {
imgView = new ImageView(ctx);
imgView.setLayoutParams(new GridView.LayoutParams(85,85));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setPadding(5, 5, 5, 5);
} else {
imgView = (ImageView) v;
}
imgView.setImageResource(img[position]);
return imgView;
}
}
}
please help me and repair mycode.big thanks

Categories

Resources