我正在尝试在屏幕上创建一条连接2个ImageButtons的线,这样该线将从第一个按钮开始,到第二个按钮结束。我将按钮放在屏幕上的xml文件中,我试图编写一种方法来绘制一组线,当我使用不同的屏幕尺寸时,这些线将正确缩放。我是否必须手动为每种不同的屏幕尺寸设置线路?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
}
super.onCreate(savedInstanceState);
size = 0;
View v = (RelativeLayout)inflater.inflate(R.layout.minor_fragment_two, container, false);
final ImageButton one = (ImageButton)v.findViewById(R.id.one);
final ImageButton two = (ImageButton)v.findViewById(R.id.two);
final ImageButton three = (ImageButton)v.findViewById(R.id.three);
final ImageButton four = (ImageButton)v.findViewById(R.id.four);
final ImageButton five = (ImageButton)v.findViewById(R.id.five);
final ImageButton six = (ImageButton)v.findViewById(R.id.six);
final ImageButton seven = (ImageButton)v.findViewById(R.id.seven);
final ImageButton eight = (ImageButton)v.findViewById(R.id.eight);
imageView = (ImageView) v.findViewById(R.id.imView);
//Display currentDisplay = getWindowManager().getDefaultDisplay();
Display currentDisplay = ((WindowManager) v.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float dw = currentDisplay.getWidth();
float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(10);
onex = one.getX();
oney = one.getY();
twox = two.getX();
twoy = two.getY();
Toast.makeText(getActivity(),
"Position :" + onex + " " + oney + " " + twox + " " + twoy, Toast.LENGTH_LONG)
.show();
canvas.drawLine(onex,oney,twox,twoy,paint);
imageView.setImageBitmap(bitmap);
//return (LinearLayout)inflater.inflate(R.layout.minor_fragment_two, container, false);
return v;
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/reduced"
android:weightSum="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imView"/>
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/one"
android:background="@drawable/first_alpha"
android:layout_marginTop="20dp"
android:layout_marginLeft="170dp"
android:layout_gravity="center_horizontal" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/two"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_marginLeft="120dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/three"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="210dp"
android:layout_marginLeft="85dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/four"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="330dp"
android:layout_marginLeft="120dp" />
<ImageButton
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/five"
android:background="@drawable/first_alpha"
android:layout_gravity="right"
android:layout_marginTop="360dp"
android:layout_marginLeft="145dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/six"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="380dp"
android:layout_marginLeft="50dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/seven"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="410dp"
android:layout_marginLeft="190dp" />
<ImageButton
android:layout_width="41dp"
android:layout_height="41dp"
android:id="@+id/eight"
android:background="@drawable/first_alpha"
android:layout_gravity="center_horizontal"
android:layout_marginTop="460dp"
android:layout_marginLeft="135dp" />
</RelativeLayout>
答案 0 :(得分:0)
尽管我理解你的问题,你可以尝试这样:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:text="One"
android:textColor="@color/white"
android:textSize="16sp" />
<View
android:id="@+id/v1"
android:layout_width="20dp"
android:layout_height="2dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/btnOne"
android:background="#000000" />
<Button
android:id="@+id/btnTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/v1"
android:background="#000000"
android:text="Two"
android:textColor="@color/white"
android:textSize="16sp" />
</RelativeLayout>
如果我弄错了,请告诉我。
答案 1 :(得分:0)
我认为您可以使用此代码,它适用于所有设备。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="button1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="button2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/btn2"
android:layout_toRightOf="@id/btn1"
android:background="@color/blue" />
</RelativeLayout>
答案 2 :(得分:0)
您没有说出图片是如何关闭的,也不是发布截图。但问题可能在于您获得的显示宽度和高度,但这与活动的可绘制区域不同(操作栏,顶部和底部的系统区域都包含在内)。
不要获取显示的宽度和高度,而是等待布局完成事件在那里进行绘制并创建与imView大小匹配的位图
ViewTreeObserver vt = view.getViewTreeObserver();
vt.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
float dw = imView.getWidth();
float dh = imView.getHeight();
// Do your drawing here...
}
});