我有问题!我加载了一个大图像(1600x1200),当应用程序启动时,视图就是左上角。 我想在它开始时显示中心或其他位置。 或者如果我给出了某些坐标并从这一点开始查看会更好。 你有什么想法吗?
提前致谢!
答案 0 :(得分:0)
您没有向我们展示任何代码,因此我们不了解您的布局或限制,但假设整个屏幕上有ImageView
;解决此问题的一种方法是让ScrollView
与ImageView
- 孩子在一起。然后,您可以使用ScrollView
- 方法scrollTo(int x, int y)
滚动到任意位置。
在此处阅读更多内容:http://developer.android.com/reference/android/widget/ScrollView.html
答案 1 :(得分:0)
我的代码是:
public Scroll(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
mGestureDetector = new GestureDetector(this);
mScale = 1;
mZoomController = new ZoomButtonsController(this);
mZoomController.setAutoDismissed(true);
mZoomController.setOnZoomListener(this);
mZoomController.setZoomSpeed(25);
mZoomController.setZoomInEnabled(mScale < MAX_ZOOM);
mZoomController.setZoomOutEnabled(mScale > MIN_ZOOM);
makeZoomLabel(context, mZoomController);
mZoomFormat = NumberFormat.getPercentInstance();
mZoomLabel.setText("Zoom: " + mZoomFormat.format(mScale));
mPicture = makeContent();
setVerticalScrollBarEnabled(true);
setHorizontalScrollBarEnabled(true);
TypedArray a = context.obtainStyledAttributes(R.styleable.Scroll);
initializeScrollbars(a);
a.recycle();
mMatrix = new Matrix();
mDst = new float[2];
}
public Scroll(Context context) {
this(context, null);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mZoomController.setVisible(false);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
if (mScroller.computeScrollOffset()) {
mX = mScroller.getCurrX();
mY = mScroller.getCurrY();
invalidate();
}
mMatrix.reset();
mMatrix.preTranslate(mX * mScale, mY * mScale);
int w = getWidth();
int h = getHeight();
float pivotX = Math.max(Math.min(-mX, w / 2), 2 * w - WIDTH - mX);
float pivotY = Math.max(Math.min(-mY, h / 2), 2 * h - HEIGHT - mY);
mMatrix.preScale(mScale, mScale, pivotX, pivotY);
canvas.concat(mMatrix);
// draw content
mPicture.draw(canvas);
canvas.restore();
}
@Override
protected int computeHorizontalScrollExtent() {
return Math.round(computeHorizontalScrollRange() * getWidth() / (WIDTH * mScale));
}
@Override
protected int computeHorizontalScrollOffset() {
mMatrix.mapPoints(mDst, SRC);
float x = -mDst[0] / mScale;
return Math.round(computeHorizontalScrollRange() * x / WIDTH);
}
@Override
protected int computeVerticalScrollExtent() {
return Math.round(computeVerticalScrollRange() * getHeight() / (HEIGHT * mScale));
}
@Override
protected int computeVerticalScrollOffset() {
mMatrix.mapPoints(mDst, SRC);
float y = -mDst[1] / mScale;
return Math.round(computeVerticalScrollRange() * y / HEIGHT);
}
public boolean onDown(MotionEvent e) {
mZoomController.setVisible(false);
return true;
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
int minX = (int) (getWidth() - WIDTH);
int minY = (int) (getHeight() - HEIGHT);
mScroller.fling((int) mX, (int) mY, (int) velocityX, (int) velocityY, minX, 0, minY, 0);
invalidate();
return true;
}
public void onLongPress(MotionEvent e) {
mZoomController.setVisible(true);
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
mX -= distanceX / mScale;
mY -= distanceY / mScale;
mX = Math.max(getWidth() - WIDTH, Math.min(0, mX));
mY = Math.max(getHeight() - HEIGHT, Math.min(0, mY));
invalidate();
return true;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
public void onVisibilityChanged(boolean visible) {
}
public void onZoom(boolean zoomIn) {
mScale += zoomIn? 0.1 : -0.1;
mScale = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, mScale));
mZoomLabel.setText("Zoom: " + mZoomFormat.format(mScale));
invalidate();
mZoomController.setZoomInEnabled(mScale != MAX_ZOOM);
mZoomController.setZoomOutEnabled(mScale != MIN_ZOOM);
}
private void makeZoomLabel(Context context, ZoomButtonsController zoomController) {
ViewGroup container = zoomController.getContainer();
View controls = zoomController.getZoomControls();
LayoutParams p0 = controls.getLayoutParams();
container.removeView(controls);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
mZoomLabel = new TextView(context);
mZoomLabel.setPadding(12, 0, 12, 0);
mZoomLabel.setTypeface(Typeface.DEFAULT_BOLD);
mZoomLabel.setTextColor(0xff000000);
PaintDrawable d = new PaintDrawable(0xeeffffff);
d.setCornerRadius(6);
mZoomLabel.setBackgroundDrawable(d);
mZoomLabel.setTextSize(20);
mZoomLabel.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
p1.gravity = Gravity.CENTER_HORIZONTAL;
layout.addView(mZoomLabel, p1);
layout.addView(controls);
container.addView(layout, p0);
}
private Picture makeContent() {
Picture picture = new Picture();
Canvas c = picture.beginRecording(WIDTH, HEIGHT);
//draw something
picture.endRecording();
return picture;
}
}