2011년 9월 23일 금요일

Gallery의 Image 무한 루프

public class HelloGallery extends Activity {
   
public final static int ADDED_EXTRA = 6;
public final static Integer[] mImageIds = {
            R.drawable.sample_1,
            R.drawable.sample_2,
            R.drawable.sample_3,
            R.drawable.sample_4,
            R.drawable.sample_5,
            R.drawable.sample_6,
            R.drawable.sample_7
    };
Gallery g;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
if (position < ADDED_EXTRA / 2)
g.setSelection(mImageIds.length + 2, false);
if (position > mImageIds.length + ADDED_EXTRA / 2) {
g.setSelection(4, false);
}
}

public void onNothingSelected(AdapterView<?> parent) {
g.setSelection(3, false);
}
        });
       
        g.setOnItemClickListener(new OnItemClickListener(){

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (position < ADDED_EXTRA / 2) {
    position = mImageIds.length- (ADDED_EXTRA / 2 - position);
    } else if (position >= (mImageIds.length + (ADDED_EXTRA / 2))) {
    position = position - (mImageIds.length + (ADDED_EXTRA / 2));
    } else {
    position -= (ADDED_EXTRA / 2);
    }

Toast.makeText(HelloGallery.this, "position: " + position, Toast.LENGTH_SHORT).show();
}
        });
       
    }
    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
//            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
//            mGalleryItemBackground = a.getResourceId(
//                   R.styleable.Gallery1_android_galleryItemBackground, 0);
//            a.recycle();
        }

        public int getCount() {
            return mImageIds.length + ADDED_EXTRA;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);
           
            if (position < ADDED_EXTRA / 2) {
    position = mImageIds.length- (ADDED_EXTRA / 2 - position);
    } else if (position >= (mImageIds.length + (ADDED_EXTRA / 2))) {
    position = position - (mImageIds.length + (ADDED_EXTRA / 2));
    } else {
    position -= (ADDED_EXTRA / 2);
    }
                     
            i.setImageResource(mImageIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(150, 100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);
           
            return i;
        }
    }
}

2011년 9월 7일 수요일

어플리케이션 바로가기 아이콘 등록 및 해제

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />



public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.setClassName(mContext, getClass().getName());
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(mContext, R.drawable.icon));
intent.putExtra("duplicate", false);
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);

Toast.makeText(mContext, "Install shortcut", Toast.LENGTH_SHORT).show();
finish();
break;
case R.id.button2:
Intent shortcutUnIntent = new Intent(Intent.ACTION_MAIN);
shortcutUnIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutUnIntent.setClassName(mContext, getClass().getName());
shortcutUnIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

Intent i = new Intent();
i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutUnIntent);
i.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCutName);
i.putExtra("duplicate", false);
i.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(i);

Toast.makeText(mContext, "UnInstall shortcut", Toast.LENGTH_SHORT).show();
finish();
break;
}
}