嗨我正在做一个应用程序在这里我需要显示图像水平scrollview.each图像我需要显示整个页面。所以我使用画廊我显示图像其工作正常。但当我滚动或移动画廊图像意味着时间更多的图像一次滚动。但我需要当我滚动图像时,我需要一次滚动一个图像。有人建议使用onfling方法。我使用那种方法。但我没有得到结果。所以任何人都帮助我,我做错了。
Gallerynew.class:
public class Gallerynew extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + " of Antartica", Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
int kEvent;
if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
}
else{ //Otherwise scrolling right
kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(kEvent, null);
return true;
}
}
答案 0 :(得分:1)
更改您的类,如下所示,创建一个自定义类,扩展库并添加fling方法并将xml文件更改为xml以下。
com.gallery1
public class Gallerynew extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + " of Antartica", Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
customclass:Gallery1.java
public class Gallery1 extends Gallery {
public Gallery1(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
} Xml文件:
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
/&GT;
答案 1 :(得分:0)
我建议使用ViewPager。它完全符合您的要求,并且位于兼容包中,因此如果您定位它们,可以将它用于早期的Android版本。