我使用CanvasView
将图像放在上面。在CanvasView
之后我在布局文件上放了一些按钮。
这是我的CanvasView代码,
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/**
* Created by user on 4/28/2016.
*/
public class CanvasView extends View {
public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
Context context;
private Paint mPaint;
private float mX, mY;
private static final float TOLERANCE = 5;
public CanvasView(Context c, AttributeSet attrs) {
super(c, attrs);
context = c;
mPath = new Path();
mPaint = new Paint();
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.CYAN);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// your Canvas will draw onto the defined Bitmap
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
// override onDraw
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Drawable d = getResources().getDrawable(R.drawable.circle_1);
int canvasHeight= mCanvas.getHeight();
int canvasWidth= mCanvas.getWidth();
Log.d("Height - "," / "+mCanvas.getHeight());
Log.d("Width - "," / "+mCanvas.getWidth());
// DisplayMetrics displaymetrics = new DisplayMetrics();
// ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
// int height = displaymetrics.heightPixels;
// int width = displaymetrics.widthPixels;
float h=canvasHeight/2;
float w=canvasWidth/2;
float r=canvasWidth/2;
d.setBounds(0, 0, canvasWidth, (canvasHeight / 8) * 5);
d.draw(canvas);
// canvas.drawCircle(w, h, r, mPaint);
}
}
这是我的布局文件,
<?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="match_parent">
<com.example.co.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="@+id/llParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/signature_canvas"
android:layout_marginTop="50dp"
android:orientation="horizontal">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/step18button1"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:background="@drawable/button_border"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/step18button2"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignTop="@+id/step18button1"
android:layout_toRightOf="@+id/step18button1"
android:layout_toEndOf="@+id/step18button1"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/step18button3"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button2"
android:layout_toRightOf="@+id/step18button2"
android:layout_toEndOf="@+id/step18button2"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/step18button4"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button3"
android:layout_toRightOf="@+id/step18button3"
android:layout_toEndOf="@+id/step18button3"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/step18button5"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button4"
android:layout_toRightOf="@+id/step18button4"
android:layout_toEndOf="@+id/step18button4"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/step18button6"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button5"
android:layout_toRightOf="@+id/step18button5"
android:layout_toEndOf="@+id/step18button5"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/step18button7"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button6"
android:layout_toRightOf="@+id/step18button6"
android:layout_toEndOf="@+id/step18button6"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/step18button8"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button7"
android:layout_toRightOf="@+id/step18button7"
android:layout_toEndOf="@+id/step18button7"
android:background="@drawable/button_border"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="39dp"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/step18button9"
android:textSize="13dp"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:layout_alignBottom="@+id/step18button8"
android:layout_toRightOf="@+id/step18button8"
android:layout_toEndOf="@+id/step18button8"
android:background="@drawable/button_border"/>
</LinearLayout>
<EditText
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="@+id/step18editText"
android:paddingLeft="10dp"
android:layout_below="@+id/llParent"
android:layout_alignLeft="@+id/llParent"
android:layout_alignStart="@+id/llParent"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="@+id/step18editText2"
android:layout_alignTop="@+id/step18editText"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="@+id/step18editText3"
android:paddingRight="10dp"
android:layout_alignTop="@+id/step18editText2"
android:layout_alignRight="@+id/step18textView5"
android:layout_alignEnd="@+id/step18textView5"
android:layout_marginRight="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content25"
android:id="@+id/step18textView2"
android:textAlignment="textStart"
android:textSize="18dp"
android:paddingLeft="10dp"
android:layout_alignTop="@+id/step18textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content26"
android:id="@+id/step18textView4"
android:textSize="18dp"
android:textAlignment="center"
android:layout_alignTop="@+id/step18textView5"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content27"
android:id="@+id/step18textView5"
android:textSize="18dp"
android:textAlignment="textEnd"
android:paddingRight="10dp"
android:layout_below="@+id/step18editText3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这就是我使用CanvasView
创建图像的方式,
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by user on 5/23/2016.
*/
public class StepTwentyOneFragment extends Fragment {
private CanvasView customCanvas;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.step21_fragment, container, false);
customCanvas=(CanvasView)v.findViewById(R.id.signature_canvas);
return v;
}
public static StepTwentyOneFragment newInstance() {
StepTwentyOneFragment f = new StepTwentyOneFragment();
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);
}
}
}
当我启动此应用程序时,我看不到CanvasView
下方的按钮。现在我的问题是,为什么我看不到按钮?
如何减少CanvasView
的高度以查看按钮?
答案 0 :(得分:1)
您将CanvasView设置为android:layout_height="match_parent"
和LinearLayout android:layout_below="@+id/signature_canvas"
。所以CanvasView覆盖了整个屏幕,而LinearLayout就在屏幕外面。
根据您的设计需要,将CanvasView更改为wrap_content
或某个固定高度。
答案 1 :(得分:1)
将
Canvas View
android:layout_height="match_parent"
更改为android:layout_height="wrap_content
。或者将一些固定Height
提供给Canvas View
。
请参阅此内容。
<com.example.co.CanvasView
android:id="@+id/signature_canvas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />