我使用自定义ImageView
类使插入的图像适合屏幕宽度,但保持纵横比。它似乎适用于我使用此自定义ImageView
的所有其他地方,但不是这一页。无法弄明白/刮擦头。提前谢谢!
UrlImageViewHelper
自定义ImageView类
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
包含ImageView的片段(剪下所有不必要的代码)
public class MainFragment extends Fragment {
private AspectRatioImageView rateMember;
static MainFragment newInstance() {
MainFragment fragment = new MainFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
rateMember = (AspectRatioImageView) view.findViewById(R.id.rate_picture);
rateMember.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ViewMemberFragment fragment = ViewMemberFragment.newInstance(picId);
ft.replace(R.id.fragment_container, fragment);
ft.addToBackStack(null);
ft.commit();
}
});
if (GlobalMethods.isNetworkAvailable(getActivity())) { new GetRandomMemberTask().execute(); }
else { Toast.makeText(getActivity(), R.string.no_internet, Toast.LENGTH_SHORT).show(); }
return view;
}
private class GetRandomMemberTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
btnVoteDown.setVisibility(View.GONE);
btnVoteUp.setVisibility(View.GONE);
btnNextPic.setVisibility(View.GONE);
}
protected Void doInBackground(Void... params) {
JSONWriter writer = new JSONWriter(getActivity());
writer.createJsonForGetPic(0);
HttpPostRequest post = new HttpPostRequest(getActivity());
post.createPost(HttpPostRequest.GET_PIC_URL);
post.addJSON(JSONWriter.FILENAME_GET_PIC);
String jsonString = post.sendPostReturnJson();
JSONReader reader = new JSONReader(getActivity());
member = reader.getMemberObject(jsonString);
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
caption.setText(member.getCaption());
UrlImageViewHelper.setUrlDrawable(rateMember, member.getImageUrl(), R.drawable.image_placeholder,
new UrlImageViewCallback() {
@Override
public void onLoaded(ImageView arg0, Bitmap arg1, String arg2, boolean arg3) {
Animation popIn = AnimationUtils.loadAnimation(getActivity(), R.anim.pop_in);
rateMember.startAnimation(popIn);
}
});
picId = member.getPicId();
if (hasAlreadyVoted()) {
btnNextPic.setVisibility(View.VISIBLE);
}
else {
btnVoteDown.setVisibility(View.VISIBLE);
btnVoteUp.setVisibility(View.VISIBLE);
}
return;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<com.walintukai.derpteam.AspectRatioImageView
android:id="@+id/rate_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/desc_rate_image" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:orientation="horizontal"
android:background="@drawable/transparent_gradient_footer" >
<ImageView
android:id="@+id/btn_vote_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_alignParentLeft="true"
android:visibility="gone"
android:src="@drawable/arrow_up"
android:contentDescription="@string/desc_vote_up" />
<ImageView
android:id="@+id/btn_vote_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="80dp"
android:layout_alignParentRight="true"
android:visibility="gone"
android:src="@drawable/arrow_down"
android:contentDescription="@string/desc_vote_down" />
<ImageView
android:id="@+id/btn_next_pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:src="@drawable/arrow_next"
android:contentDescription="@string/desc_next_picture" />
</RelativeLayout>
</FrameLayout>
<com.walintukai.derpteam.CustomFontBoldTextView
android:id="@+id/caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_horizontal"
android:textAppearance="@style/caption" />
</LinearLayout>
</ScrollView>
堆栈跟踪
03-19 17:31:49.341: E/AndroidRuntime(13652): FATAL EXCEPTION: main
03-19 17:31:49.341: E/AndroidRuntime(13652): java.lang.NullPointerException
03-19 17:31:49.341: E/AndroidRuntime(13652): at com.walintukai.derpteam.AspectRatioImageView.onMeasure(AspectRatioImageView.java:24)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1456)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.ScrollView.onMeasure(ScrollView.java:387)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-19 17:31:49.341: E/AndroidRuntime(13652): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2362)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.View.measure(View.java:15395)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1985)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1226)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1399)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4553)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.Choreographer.doFrame(Choreographer.java:525)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.os.Handler.handleCallback(Handler.java:615)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.os.Looper.loop(Looper.java:137)
03-19 17:31:49.341: E/AndroidRuntime(13652): at android.app.ActivityThread.main(ActivityThread.java:4950)
03-19 17:31:49.341: E/AndroidRuntime(13652): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 17:31:49.341: E/AndroidRuntime(13652): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 17:31:49.341: E/AndroidRuntime(13652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
03-19 17:31:49.341: E/AndroidRuntime(13652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
03-19 17:31:49.341: E/AndroidRuntime(13652): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:5)
您最初的ImageView
内没有抽奖,因此getDrawable()
会返回null。
您可以将代码更改为:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (getDrawable() != null {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
} else {
setMeasuredDimension(0, 0);
}
}