- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下 imagedetails.xml 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/inLay"
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="horizontal" >
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/innerLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/controlled_medication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/as_needed_medication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
对于这个 xml 文件,我编写了以下代码:
public class ImageDetails extends Activity {
LinearLayout asthmaActionPlan, controlledMedication, asNeededMedication,
rescueMedication, yourSymtoms, yourTriggers, wheezeRate, peakFlow;
LayoutParams params;
LinearLayout next, prev;
int viewWidth;
GestureDetector gestureDetector = null;
HorizontalScrollView horizontalScrollView;
ArrayList<LinearLayout> layouts;
int parentLeft, parentRight;
int mWidth;
int currPosition, prevPosition;
ImageView image1, image2;
String pid;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String url_detials = "http://www.example.com/details.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCT = "product";
private static final String TAG_PID = "id";
Private static final String TAG_IMAGE1 = "image1";
private static final String TAG_IMAGE2 = "image2";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productdetails);
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
Intent i = getIntent();
pid = i.getStringExtra(TAG_PID);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
gestureDetector = new GestureDetector(new MyGestureDetector());
controlledMedication = (LinearLayout) findViewById(R.id.controlled_medication);
asNeededMedication = (LinearLayout) findViewById(R.id.as_needed_medication);
Display display = getWindowManager().getDefaultDisplay();
mWidth = display.getWidth(); // deprecated
viewWidth = mWidth;
layouts = new ArrayList<LinearLayout>();
params = new LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);
controlledMedication.setLayoutParams(params);
asNeededMedication.setLayoutParams(params);
layouts.add(controlledMedication);
layouts.add(asNeededMedication);
horizontalScrollView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
new GetDetails().execute();
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() < e2.getX()) {
currPosition = getVisibleViews("left");
} else {
currPosition = getVisibleViews("right");
}
horizontalScrollView.smoothScrollTo(layouts.get(currPosition)
.getLeft(), 0);
return true;
}
}
public int getVisibleViews(String direction) {
Rect hitRect = new Rect();
int position = 0;
int rightCounter = 0;
for (int i = 0; i < layouts.size(); i++) {
if (layouts.get(i).getLocalVisibleRect(hitRect)) {
if (direction.equals("left")) {
position = i;
break;
} else if (direction.equals("right")) {
rightCounter++;
position = i;
if (rightCounter == 2)
break;
}
}
}
return position;
}
class GetDetails extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ImageDetails.this);
pDialog.setMessage("Loading Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// pDialog.show();
}
@Override
protected String doInBackground(String... arg) {
runOnUiThread(new Runnable() {
public void run() {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", pid));
JSONObject json = jsonParser.makeHttpRequest(
url_detials, "GET", params);
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT);
JSONObject product = productObj.getJSONObject(0);
Bitmap bitmap = null;
Bitmap bitmap2 = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(
product.getString(TAG_IMAGE1))
.openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
image1.setImageBitmap(bitmap);
// Download Image from URL
InputStream input2 = new java.net.URL(
product.getString(TAG_IMAGE2))
.openStream();
// Decode Bitmap
bitmap2 = BitmapFactory.decodeStream(input2);
image2.setImageBitmap(bitmap2);
} catch (Exception e) {
e.printStackTrace();
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
现在它会在水平 View 中给我输出,我可以通过手势触摸更改图像。
现在,当我选择任何图像时,它将以全屏模式打开并带有放大/缩小选项。
我尝试了以下方法:
image1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
image1.buildDrawingCache();
Bitmap bitmap = image1.getDrawingCache();
Intent intent = new Intent(getApplicationContext(),
Imagesview.class);
intent.putExtra("BitmapImage", bitmap);
}
});
我正在向其他 Activity 发送图像,但它没有打开新 Activity 。
我收到以下错误日志:
FATAL EXCEPTION: main
Process: com.novumlogic.ideal, PID: 18636
java.lang.NullPointerException
at com.novumlogic.ideal.ProductDetails$MyGestureDetector.onFling(ProductDetails.java:213)
at android.view.GestureDetector.onTouchEvent(GestureDetector.java:610)
at com.novumlogic.ideal.ProductDetails$1.onTouch(ProductDetails.java:133)
at android.view.View.dispatchTouchEvent(View.java:7772)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2316)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2013)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2145)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1577)
at android.app.Activity.dispatchTouchEvent(Activity.java:2508)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2093)
at android.view.View.dispatchPointerEvent(View.java:7973)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4384)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4255)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3927)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3984)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3851)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3820)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3828)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3801)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6116)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6096)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6050)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6246)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5333)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
那么,如何使用放大/缩小选项全屏显示图像?
最佳答案
下面的类可以用作 ImageView 用于放大/缩小以及拖动...
public class TouchImageView extends ImageView {
Matrix 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 last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;
int viewWidth, viewHeight;
static final int CLICK = 3;
float saveScale = 1f;
protected float origWidth, origHeight;
int oldMeasuredWidth, oldMeasuredHeight;
ScaleGestureDetector mScaleDetector;
Context context;
public TouchImageView(Context context) {
super(context);
sharedConstructing(context);
}
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructing(context);
}
private void stopInterceptEvent()
{
getParent().requestDisallowInterceptTouchEvent(true);
}
private void startInterceptEvent()
{
getParent().requestDisallowInterceptTouchEvent(false);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix = new Matrix();
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(curr);
start.set(last);
mode = DRAG;
stopInterceptEvent();
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float fixTransX = getFixDragTrans(deltaX, viewWidth, origWidth * saveScale);
float fixTransY = getFixDragTrans(deltaY, viewHeight, origHeight * saveScale);
matrix.postTranslate(fixTransX, fixTransY);
fixTrans();
last.set(curr.x, curr.y);
float transX = m[Matrix.MTRANS_X];
if((int) (getFixTrans(transX, viewWidth, origWidth * saveScale) + fixTransX) == 0)
startInterceptEvent();
else
stopInterceptEvent();
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
startInterceptEvent();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
public void setMaxZoom(float x) {
maxScale = x;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float mScaleFactor = detector.getScaleFactor();
float origScale = saveScale;
saveScale *= mScaleFactor;
if (saveScale > maxScale) {
saveScale = maxScale;
mScaleFactor = maxScale / origScale;
} else if (saveScale < minScale) {
saveScale = minScale;
mScaleFactor = minScale / origScale;
}
if (origWidth * saveScale <= viewWidth || origHeight * saveScale <= viewHeight)
matrix.postScale(mScaleFactor, mScaleFactor, viewWidth / 2, viewHeight / 2);
else
matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
fixTrans();
return true;
}
}
void fixTrans() {
matrix.getValues(m);
float transX = m[Matrix.MTRANS_X];
float transY = m[Matrix.MTRANS_Y];
float fixTransX = getFixTrans(transX, viewWidth, origWidth * saveScale);
float fixTransY = getFixTrans(transY, viewHeight, origHeight * saveScale);
if (fixTransX != 0 || fixTransY != 0)
matrix.postTranslate(fixTransX, fixTransY);
}
float getFixTrans(float trans, float viewSize, float contentSize) {
float minTrans, maxTrans;
if (contentSize <= viewSize) {
minTrans = 0;
maxTrans = viewSize - contentSize;
} else {
minTrans = viewSize - contentSize;
maxTrans = 0;
}
if (trans < minTrans)
return -trans + minTrans;
if (trans > maxTrans)
return -trans + maxTrans;
return 0;
}
float getFixDragTrans(float delta, float viewSize, float contentSize) {
if (contentSize <= viewSize) {
return 0;
}
return delta;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
viewWidth = MeasureSpec.getSize(widthMeasureSpec);
viewHeight = MeasureSpec.getSize(heightMeasureSpec);
//
// Rescales image on rotation
//
if (oldMeasuredHeight == viewWidth && oldMeasuredHeight == viewHeight
|| viewWidth == 0 || viewHeight == 0)
return;
oldMeasuredHeight = viewHeight;
oldMeasuredWidth = viewWidth;
if (saveScale == 1) {
//Fit to screen.
float scale;
Drawable drawable = getDrawable();
if (drawable == null || drawable.getIntrinsicWidth() == 0 || drawable.getIntrinsicHeight() == 0)
return;
int bmWidth = drawable.getIntrinsicWidth();
int bmHeight = drawable.getIntrinsicHeight();
Log.d("bmSize", "bmWidth: " + bmWidth + " bmHeight : " + bmHeight);
float scaleX = (float) viewWidth / (float) bmWidth;
float scaleY = (float) viewHeight / (float) bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale, scale);
// Center the image
float redundantYSpace = (float) viewHeight - (scale * (float) bmHeight);
float redundantXSpace = (float) viewWidth - (scale * (float) bmWidth);
redundantYSpace /= (float) 2;
redundantXSpace /= (float) 2;
matrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = viewWidth - 2 * redundantXSpace;
origHeight = viewHeight - 2 * redundantYSpace;
setImageMatrix(matrix);
}
fixTrans();
}
}
替换你的 ImageView
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
用这个:
<com.pkg.TouchImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You can use RecyclingImageView as a super class here for your app efficiency. It will also work with Android native ImageView
希望对您有所帮助。
关于android - 如何在触摸图像时放大/缩小全屏查看图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29028429/
我正在开发这样的摄影应用程序: https://play.google.com/store/apps/details?id=com.photo.editor.collage.maker.photobl
我正在尝试创建一个可以像在 Excel 中一样放大和缩小的 QTableView。 此处提出了类似的问题:Zooming function on a QWidget 但是,我在 PyQt 而不是 C
我想找到放大/缩小 TVirtualStringTree 的“最佳方法”。 “放大”的意思是模仿放大镜。 必须通过优先使用 TVirtualStringTree 控件中专门用于此目的的属性/方法来理解
有没有办法在emacs上放大和缩小(动态改变字体大小,相当流畅)? 最佳答案 尝试 C-x C-+ 和 C-x C--;即 Control-x Control-减号/Control-再加上。 在一个组
我在使用 Leaflet 时遇到问题。我在 map 上找到了一堆标记(在我的例子中它代表高尔夫球场)。但是,当我放大/缩小时,标记在 map 上移动。 浏览网页后,解决方案似乎是 iconAnchor
我在使用 Leaflet 时遇到问题。我在 map 上找到了一堆标记(在我的例子中它代表高尔夫球场)。但是,当我放大/缩小时,标记在 map 上移动。 浏览网页后,解决方案似乎是 iconAnchor
我正在开发一款非常低分辨率的游戏,我需要放大它才能看到。我知道我可以使用 Graphics.scale(float x, float y) 但我想放大到中心。如何缩放中心的图形?有没有更简单的方法来制
我有这种方法可以向前/向后和向左/向右平移相机。我不确定为什么,但是是什么导致相机在靠近地形放大时移动得很好,但在缩小时移动得非常慢? 这是我平移相机的方式: void CameraPan(){
我正在尝试像这样进行缩放:zoom the imageview on the double click in viewflipper in android 我尝试将该代码改编为我的项目,但它不起作用.
$(window).height() 获取页面加载时窗口的高度。但是,在放大/缩小时,我想要新的视点高度。这可能吗? 它可以很好地调整普通浏览器的大小,但我想在移动设备上放大/缩小不会触发 $(win
我正在尝试制作一个显示图像、 block 文本和按钮的面板,与下一个模型上显示的面板保持相似的比率,无论页面大小/缩放比例是多少。 我首先尝试使用横写文本的图像(即不对文本、图像和背景使用不同的部分或
Blockquote 大家好,请问是否可以在此代码中添加放大、缩小功能?我对 html 很陌生。任何帮助将不胜感激。 Career Center
我有一个由 3 个元素组成的搜索栏。 首先 a 保存从下拉列表中选择的值。 第二个清除选择的元素 第三个供用户在下拉菜单之间切换。 我的问题 - 当我缩放超过 100% 时,img 和按钮元素变大了一
我目前遇到网站页脚的问题。 当以 100% 大小(正常大小)处理时,页脚对齐得很好。但是,当我调整它的大小时,它完全不对齐并位于左侧,它需要保持居中。 屏幕截图: 相关 CSS: /* Dark bl
我实现了 3 个按钮来允许用户放大、缩小和返回到默认大小。 脚本可以进行放大和缩小,但我还有 2 个问题无法解决。 第一个问题: 每次放大时,要缩放的图像仍然到达我的窗口的限制,当图像左右没有任何空间
即使我在网页上放大或缩小,我也试图让 shadowbox 保持在同一个地方。我尝试以百分比而不是像素为单位来制作尺寸,但我不知道应该使用哪种属性组合。 这是我想要实现的结果:来自这里的外部阴影框效果h
我有点困惑。我按照 Apple 的建议放大 ScrollView ,但没有任何反应。 代码: import UIKit import SpriteKit class ScrollViewControl
我正在尝试通过导航实现类似 Bootstrap 的 header ,但我刚刚发现当我缩小窗口时, header 内的内容变得困惑并且 header 也最小化而不是保持相同的大小。我已经在页眉上使用了
我正在开发一个包含许多 UIView 的 iOS 应用程序。 UINavigation 用于在这些 View 之间导航。 我的一个 UIView 包含 UITextField。我的问题是,当我缩放时,
我正在尝试从 FITS 文件中绘制一些数据,我想知道是否有人知道如何关注绘图轴的某些区域?下面是一些示例代码: import pyfits from matplotlib import pyplot
我是一名优秀的程序员,十分优秀!