我准备了一个自定义图库视图,它从磁盘上的任何位置获取图像并在Drawable()中滑动它们。但问题是你在图片中看到的,当我点击一个图像时,它会移出它的框架。
这是在我从Integer [] pic = {R.drawable .....}转换来源后发生的 到向量中的Integer数组,并将imagesource(arg [0])更改为
iv.setImageDrawable(mySDCardImages.get(arg0).getDrawable());
LinearLayout imageView;
Integer [] mThumbIds;
Vector<ImageView> mySDCardImages;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_image_explore);
final Vector<ImageView> mySDCardImages;
mySDCardImages = new Vector<ImageView>();
List<Integer> drawablesId = new ArrayList<Integer>();
int picIndex=12345;
File sdDir = new File("/sdcard/Pictures");
File[] sdDirFiles = sdDir.listFiles();
for(File singleFile : sdDirFiles)
{
ImageView myImageView = new ImageView(this);
myImageView.setImageDrawable(Drawable.createFromPath(singleFile.getAbsolutePath()));
myImageView.setId(picIndex);
picIndex++;
drawablesId.add(myImageView.getId());
mySDCardImages.add(myImageView);
}
Integer [] mThumbIds = (Integer[])drawablesId.toArray(new Integer[0]);
this.mThumbIds = mThumbIds;
this.mySDCardImages=mySDCardImages;
try {
// InputStream in = (new URL("www.google.com").openStream());
} catch (Exception e) {
e.getMessage();
}
Gallery ga = (Gallery) findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (LinearLayout) findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(
getBaseContext(),
"You have selected picture " + (arg2 + 1)
+ " of Antartica",
Toast.LENGTH_SHORT).show();
try {
imageView.removeAllViews();
} catch (Exception e) {
e.getMessage();
}
TouchImageView touchImageView = new TouchImageView(
GalleryView.this);
touchImageView.setImageDrawable(mySDCardImages.get(arg2).getDrawable());;
LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
imageView.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
touchImageView.setLayoutParams(lp);
imageView.addView(touchImageView);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
@Override
public int getCount() {
return mThumbIds.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 iv = new ImageView(ctx);
iv.setImageDrawable(mySDCardImages.get(arg0).getDrawable());
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
这是我的touchimage适配器以防万一:
public class TouchImageView extends ImageView {
private static final String TAG = "Touch";
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
Context context;
public TouchImageView(Context context) {
super(context);
super.setClickable(true);
this.context = context;
matrix.setTranslate(1f, 1f);
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent rawEvent) {
WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
// Dump touch event to log
// if (Viewer.isDebug == true){
// dumpEvent(event);
// }
// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
savedMatrix.set(matrix);
start.set(event.getX(), event.getY());
Log.d(TAG, "mode=DRAG");
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM");
}
break;
case MotionEvent.ACTION_UP:
int xDiff = (int) Math.abs(event.getX() - start.x);
int yDiff = (int) Math.abs(event.getY() - start.y);
if (xDiff < 8 && yDiff < 8){
performClick();
}
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
Log.d(TAG, "mode=NONE");
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
matrix.set(savedMatrix);
matrix.postTranslate(event.getX() - start.x, event.getY() -
start.y);
} else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
setImageMatrix(matrix);
return true; // indicate event was handled
}
});
}
public void setImage(Bitmap bm, int displayWidth, int displayHeight) {
super.setImageBitmap(bm);
//Fit to screen.
float scale;
if ((displayHeight / bm.getHeight()) >= (displayWidth / bm.getWidth())){
scale = (float)displayWidth / (float)bm.getWidth();
} else {
scale = (float)displayHeight / (float)bm.getHeight();
}
savedMatrix.set(matrix);
matrix.set(savedMatrix);
matrix.postScale(scale, scale, mid.x, mid.y);
setImageMatrix(matrix);
// Center the image
float redundantYSpace = (float)displayHeight - (scale * (float)bm.getHeight()) ;
float redundantXSpace = (float)displayWidth - (scale * (float)bm.getWidth());
redundantYSpace /= (float)2;
redundantXSpace /= (float)2;
savedMatrix.set(matrix);
matrix.set(savedMatrix);
matrix.postTranslate(redundantXSpace, redundantYSpace);
setImageMatrix(matrix);
}
/** Show an event in the LogCat view, for debugging */
@SuppressWarnings("unused")
private void dumpEvent(WrapMotionEvent event) {
// ...
String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE",
"POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
StringBuilder sb = new StringBuilder();
int action = event.getAction();
int actionCode = action & MotionEvent.ACTION_MASK;
sb.append("event ACTION_").append(names[actionCode]);
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
sb.append("(pid ").append(
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
sb.append(")");
}
sb.append("[");
for (int i = 0; i < event.getPointerCount(); i++) {
sb.append("#").append(i);
sb.append("(pid ").append(event.getPointerId(i));
sb.append(")=").append((int) event.getX(i));
sb.append(",").append((int) event.getY(i));
if (i + 1 < event.getPointerCount())
sb.append(";");
}
sb.append("]");
Log.d(TAG, sb.toString());
}
/** Determine the space between the first two fingers */
private float spacing(WrapMotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, WrapMotionEvent event) {
// ...
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}
}
对不起它太久了,你们之前有过这样的经历吗?你们知道问题出在哪里吗?希望能发布图片
此外,图像不适合屏幕。不再......
答案 0 :(得分:0)
imageView
,该变量实际上是使用LinearLayout
id标记的R.id.ImageView01
。我的建议如下:
TouchImageView
并使用TouchImageView。它非常可靠,无错误,效果很好,并且为图像库提供了ImageView
所期望的所有功能。我个人在生产代码中亲自使用它,没有任何问题。ImageAdapter
实施不正确。把它丢掉吧。而是扩展ArrayAdapter<Drawable>
。通过传递mySDCardImages
并覆盖getView()
方法来强化您的观点来构建它。getView()
方法。这是我发现的快速link。即使它谈到ListViews
同样的概念也适用于Gallery
。mySDCardImages
不应该存储ImageViews
。只需让它直接存储Drawable
。mThumbIds
是什么......但是从我所看到的,只是删除它。似乎没有任何目的。Vector
课程?除非你真的需要synchronization
,否则最好坚持使用ArrayList
。