我试图在ImageView
的中间画一个圆圈。以下是我的UI代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:id="@+id/bluh">
<ImageView
android:id="@+id/image_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:layout_weight="2"
android:src="@drawable/hhh" />
</LinearLayout>
<LinearLayout
android:id="@+id/l22Parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/bluh"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/step18button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/step18textView"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="1"
android:textSize="13dp" />
<Button
android:id="@+id/step18button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/step18button1"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_weight="1"
android:text="2"
android:textSize="13dp" />
<Button
android:id="@+id/step18button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button2"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_weight="1"
android:text="3"
android:textSize="13dp" />
<Button
android:id="@+id/step18button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button3"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_weight="1"
android:text="4"
android:textSize="13dp" />
<Button
android:id="@+id/step18button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button4"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_weight="1"
android:text="5"
android:textSize="13dp" />
<Button
android:id="@+id/step18button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button5"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_weight="1"
android:text="6"
android:textSize="13dp" />
<Button
android:id="@+id/step18button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button6"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_weight="1"
android:text="7"
android:textSize="13dp" />
<Button
android:id="@+id/step18button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button7"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_weight="1"
android:text="8"
android:textSize="13dp" />
<Button
android:id="@+id/step18button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/step18button8"
android:layout_marginLeft="3dp"
android:layout_toEndOf="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_weight="1"
android:text="9"
android:textSize="13dp" />
</LinearLayout>
</LinearLayout>
下面是我的片段类
/**
* Created by user on 5/12/2016.
*/
public class GeneralizationFragment extends Fragment {
Context context;
private ImageView imageView;
private int width;
private int height;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.generalization_fragment, container, false);
createBitMap(v);
return v;
}
public static GeneralizationFragment newInstance() {
GeneralizationFragment f = new GeneralizationFragment();
Bundle b = new Bundle();
f.setArguments(b);
return f;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser) {
Activity a = getActivity();
if(a != null) a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
private void createBitMap(final View v) {
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = true;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hhh, myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
imageView = (ImageView)v.findViewById(R.id.image_background);
ViewTreeObserver vto = imageView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
imageView.getViewTreeObserver().removeOnPreDrawListener(this);
height = imageView.getMeasuredHeight();
width = imageView.getMeasuredWidth();
Log.d("GENERALIZED", "Width:" + width);
return true;
}
});
float radius = (float) (width-(width*0.2));
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(100, 100, 60, paint);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(mutableBitmap);
}
}
但是,在我从代码中删除imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
之前,我无法绘制圆圈(或圆圈不可见)。然而,这部分对我来说是一种管理。
我也不知道在中心画画。