Add function to zoom in and out the barcode

This commit is contained in:
ligi 2015-10-08 22:31:17 +02:00
parent 3efed3e588
commit 7712044547
13 changed files with 98 additions and 27 deletions

View file

@ -38,7 +38,7 @@ public class InputStreamProvider {
default:
Tracker.get().trackException("unknown scheme in ImportAsyncTask" + uri.getScheme(), false);
case "file":
return InputStreamProvider.getDefaultHttpInputStreamForUri(uri);
return InputStreamProvider.getDefaultInputStreamForUri(uri);
}
}
@ -89,7 +89,7 @@ public class InputStreamProvider {
}
public static InputStreamWithSource getDefaultHttpInputStreamForUri(final Uri uri) {
public static InputStreamWithSource getDefaultInputStreamForUri(final Uri uri) {
try {
return new InputStreamWithSource(uri.toString(), new BufferedInputStream(new URL(uri.toString()).openStream(), 4096));
} catch (IOException e) {

View file

@ -1,19 +1,17 @@
package org.ligi.passandroid.ui;
import android.content.Context;
import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.NavUtils;
import android.support.v4.app.TaskStackBuilder;
import android.text.Html;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.*;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -72,6 +70,52 @@ public class PassViewActivity extends PassViewActivityBase {
@Bind(R.id.barcode_alt_text)
TextView barcodeAlternativeText;
@OnClick(R.id.zoomIn)
void zoomIn() {
setBarCodeSize(currentBarcodeWidth + getFingerSize());
}
@OnClick(R.id.zoomOut)
void zooOut() {
setBarCodeSize(currentBarcodeWidth - getFingerSize());
}
@Bind(R.id.zoomOut)
View zoomOut;
@Bind(R.id.zoomIn)
View zoomIn;
private void setBarCodeSize(int width) {
if (width < getFingerSize() * 2) {
zoomOut.setVisibility(View.INVISIBLE);
} else {
zoomOut.setVisibility(View.VISIBLE);
}
if (width > getWindowWidth() - getFingerSize() * 2) {
zoomIn.setVisibility(View.INVISIBLE);
} else {
zoomIn.setVisibility(View.VISIBLE);
}
currentBarcodeWidth = width;
barcode_img.setLayoutParams(new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT));
}
@SuppressWarnings("deprecation")
private int getWindowWidth() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
final Point point = new Point();
getWindowManager().getDefaultDisplay().getSize(point);
return point.x;
} else {
return getWindowManager().getDefaultDisplay().getWidth();
}
}
int currentBarcodeWidth;
@Override
protected void onResume() {
super.onResume();
@ -107,7 +151,8 @@ public class PassViewActivity extends PassViewActivityBase {
if (pass.getBarCode() != null) {
final int smallestSide = AXT.at(getWindowManager()).getSmallestSide();
final Bitmap bitmap = pass.getBarCode().getBitmap(smallestSide / 3);
final Bitmap bitmap = pass.getBarCode().getBitmap(getWindowWidth());
setBitmapSafe(barcode_img, bitmap);
if (pass.getBarCode().getAlternativeText() != null) {
barcodeAlternativeText.setText(pass.getBarCode().getAlternativeText());
@ -115,6 +160,8 @@ public class PassViewActivity extends PassViewActivityBase {
} else {
barcodeAlternativeText.setVisibility(View.GONE);
}
setBarCodeSize(smallestSide / 2);
} else {
setBitmapSafe(barcode_img, null);
}
@ -173,7 +220,7 @@ public class PassViewActivity extends PassViewActivityBase {
private void addFrontFields(PassFieldList passFields) {
for (PassField field : passFields) {
final View v = getLayoutInflater().inflate(R.layout.main_field_item, frontFieldsContainer,false);
final View v = getLayoutInflater().inflate(R.layout.main_field_item, frontFieldsContainer, false);
final TextView key = (TextView) v.findViewById(R.id.key);
key.setText(field.label);
final TextView value = (TextView) v.findViewById(R.id.value);
@ -183,7 +230,7 @@ public class PassViewActivity extends PassViewActivityBase {
}
}
private static void setBitmapSafe(ImageView imageView, Bitmap bitmap) {
private void setBitmapSafe(ImageView imageView, Bitmap bitmap) {
if (bitmap != null) {
imageView.setLayoutParams(getLayoutParamsSoThatWeHaveAtLeasHalfAFinger(imageView, bitmap));
@ -198,23 +245,24 @@ public class PassViewActivity extends PassViewActivityBase {
}
@NonNull
private static ViewGroup.LayoutParams getLayoutParamsSoThatWeHaveAtLeasHalfAFinger(final ImageView imageView, final Bitmap bitmap) {
final Context context = imageView.getContext();
final int halfAFingerInPixels = context.getResources().getDimensionPixelSize(R.dimen.finger)/2 ;
final ViewGroup.LayoutParams params=imageView.getLayoutParams();
if (bitmap.getHeight()< halfAFingerInPixels) {
params.height= halfAFingerInPixels;
private ViewGroup.LayoutParams getLayoutParamsSoThatWeHaveAtLeasHalfAFinger(final ImageView imageView, final Bitmap bitmap) {
final int halfAFingerInPixels = getFingerSize() / 2;
final ViewGroup.LayoutParams params = imageView.getLayoutParams();
if (bitmap.getHeight() < halfAFingerInPixels) {
params.height = halfAFingerInPixels;
} else {
params.height= LinearLayout.LayoutParams.WRAP_CONTENT;
params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
}
return params;
}
private int getFingerSize() {
return getResources().getDimensionPixelSize(R.dimen.finger);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean res = super.onPrepareOptionsMenu(menu);
final boolean res = super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.menu_map).setVisible((optionalPass != null && optionalPass.isValid() && optionalPass.getLocations().size() > 0));
menu.findItem(R.id.menu_update).setVisible(mightPassBeAbleToUpdate(optionalPass));
return res;
@ -243,7 +291,6 @@ public class PassViewActivity extends PassViewActivityBase {
return super.onOptionsItemSelected(item);
}
@Override
public void onAttachedToWindow() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

Binary file not shown.

After

Width:  |  Height:  |  Size: 447 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 436 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 505 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 770 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 760 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 984 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 971 B

View file

@ -27,13 +27,37 @@
android:layout_height="wrap_content"
android:orientation="vertical"/>
<ImageView
android:id="@+id/barcode_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/barcode"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/zoomOut"
android:background="@drawable/clickable_bg"
android:drawableRight="@drawable/ic_action_zoom_out"
android:layout_width="0dip"
android:layout_weight="1"
android:minWidth="@dimen/finger"
android:layout_height="match_parent"/>
<ImageView
android:id="@+id/barcode_img"
tools:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@string/barcode"/>
<TextView
android:id="@+id/zoomIn"
android:drawableLeft="@drawable/ic_action_zoom_in"
android:layout_width="0dip"
android:layout_weight="1"
android:minWidth="@dimen/finger"
android:background="@drawable/clickable_bg"
android:clickable="true"
android:layout_height="match_parent"/>
</LinearLayout>
<TextView
android:id="@+id/barcode_alt_text"