我自定义了一个view,代码如下:
public class RotateView extends View { Bitmap userBmp;
Matrix matrix;
public RotateView(Context context) {
super(context);
init();
} public void init()
{
userBmp=BitmapFactory.decodeResource(getResources(), R.drawable.user);
matrix=new Matrix();
matrix.postRotate(15);
matrix.postScale(2f, 2f);
matrix.postTranslate(100f, 100f);
}
@Override
protected void onDraw(Canvas canvas) {
init();
super.onDraw(canvas);
canvas.drawBitmap(userBmp, matrix, null);
}}然后在view2main.xml中如下设置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<lc.android.rotateViewTest.RotateView
android:id="@+id/rv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
然后在activity里面如下用:
public class RotateViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2main);
}
}
运行的时候,logcat提示如下错误:
真心求指点。。
public class RotateView extends View { Bitmap userBmp;
Matrix matrix;
public RotateView(Context context) {
super(context);
init();
} public void init()
{
userBmp=BitmapFactory.decodeResource(getResources(), R.drawable.user);
matrix=new Matrix();
matrix.postRotate(15);
matrix.postScale(2f, 2f);
matrix.postTranslate(100f, 100f);
}
@Override
protected void onDraw(Canvas canvas) {
init();
super.onDraw(canvas);
canvas.drawBitmap(userBmp, matrix, null);
}}然后在view2main.xml中如下设置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<lc.android.rotateViewTest.RotateView
android:id="@+id/rv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
然后在activity里面如下用:
public class RotateViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view2main);
}
}
运行的时候,logcat提示如下错误:
真心求指点。。
public RotateView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
没人来接分么
这里面是什么,怎么不用你的View
View只是我定义的一个组件R.layout.view2main 就是包含了这个组件的XML布局