Android - 在我的活动中使用自定义视图类

时间:2014-06-30 01:00:12

标签: android xml android-layout view android-custom-view

我对Android开发很新,并尝试实现我自己的自定义视图,这将允许我在画布上使用绘图操作。我的自定义视图应该在onDraw()函数的屏幕上绘制一个矩形,但它似乎不起作用,没有绘制矩形。

我正在尝试使用自定义视图的基本主要活动:

package com.example.testcanvas;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);


  }

}

活动的XML文件,我尝试将自定义视图添加到:

片段main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testcanvas.MainActivity$PlaceholderFragment" >




</RelativeLayout>

活动main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testcanvas.MainActivity"
tools:ignore="MergeRootFrame" >

<com.example.testcanvas.CustomView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/custom_view" />

 </RelativeLayout>

最后,我的自定义视图类:

package com.example.testcanvas;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class CustomView extends SurfaceView implements SurfaceHolder.Callback {

public CustomView (Context context) {
    super(context);
}

public CustomView (Context context, AttributeSet attrs) {
    super(context);
}

@Override
public void onDraw (Canvas canvas) {
    int dCenter = 40;     //Distance from center in px, NOT in dp
    int centerX = (int)(getWidth()/2);
    int centerY = (int)(getHeight()/2);

    Paint paint = new Paint();
    paint.setColor(Color.BLUE);  //The square will draw in the color blue now


    Rect rect = new Rect(centerX-dCenter, centerY-dCenter, centerX+dCenter, centerY+dCenter);
    canvas.drawRect(rect, paint);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    setWillNotDraw(false);   //Allows us to use invalidate() to call onDraw()
    postInvalidate();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

}

有人可能指出我出错的地方吗?感谢。

0 个答案:

没有答案