如何创建无活动“的setContentView(R.layout.main)' [英] How to create an activity without 'setContentView(R.layout.main)'
问题描述
我知道它可以通过做一些像code波纹管,这里的观点是没有设置的XML文件,但创建活动是这样的:的setContentView(新MyView的(这个));
我不明白的是如何使用这个code,但还是对其进行自定义,例如,如果我想添加一个按钮code波纹管的能力,如何将我做到这一点,因为我不能只是添加一个到一个XML布局可以吗?
什么好的答案,这样将在很大程度上AP preCIATED 提前致谢!
包com.faceapp;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.PointF;
进口android.media.FaceDetector;
进口android.media.FaceDetector.Face;
进口android.os.Bundle;
进口android.view.View;
公共类FaceappActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
的setContentView(新MyView的(这个));
}
私有类MyView的扩展视图{
私人诠释ImageWidth等,imageHeight;
私人诠释numberOfFace = 5;
私人FaceDetector myFaceDetect;
私人FaceDetector.Face [] myFace;
浮动myEyesDistance;
INT numberOfFaceDetected;
位图MYBITMAP;
公共MyView的(上下文的背景下){
超(上下文);
// TODO自动生成构造函数存根
BitmapFactory.Options BitmapFactoryOptionsbfo =新BitmapFactory.Options();
BitmapFactoryOptionsbfo.in preferredConfig = Bitmap.Config.RGB_565;
MYBITMAP = BitmapFactory.de codeResource(getResources(),R.drawable.face5,
BitmapFactoryOptionsbfo);
ImageWidth等= myBitmap.getWidth();
imageHeight = myBitmap.getHeight();
myFace =新FaceDetector.Face [numberOfFace]
myFaceDetect =新FaceDetector(ImageWidth等,imageHeight,numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(MYBITMAP,myFace);
}
@覆盖
保护无效的OnDraw(帆布油画){
// TODO自动生成方法存根
canvas.drawBitmap(MYBITMAP,0,0,NULL);
油漆myPaint =新的油漆();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
的for(int i = 0; I< numberOfFaceDetected;我++)
{
面对面= myFace [I]
的PointF myMidPoint =新的PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance();
canvas.drawRect(
(INT)(myMidPoint.x - myEyesDistance)
(INT)(myMidPoint.y - myEyesDistance)
(INT)(myMidPoint.x + myEyesDistance)
(INT)(myMidPoint.y + myEyesDistance)
myPaint);
}
}
}
}
^^^^^^^^^^^^^^^ 回答
如何定位按钮和ImageView的? (理想情况下使用相对布局) 图片波纹管显示你明白我的意思: (忽略该图像是重新调整大小)
新的code:
com.test包;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.PointF;
进口android.media.FaceDetector;
进口android.media.FaceDetector.Face;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.LinearLayout;
公共类TesttActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的LinearLayout布局=新的LinearLayout(本);
Button按钮=新的按钮(这一点);
button.setText(按钮!);
layout.addView(按钮);
MyView的自定义=新的MyView的(这一点);
layout.addView(自定义);
的setContentView(布局);
}
私有类MyView的扩展视图{
私人诠释ImageWidth等,imageHeight;
私人诠释numberOfFace = 5;
私人FaceDetector myFaceDetect;
私人FaceDetector.Face [] myFace;
浮动myEyesDistance;
INT numberOfFaceDetected;
位图MYBITMAP;
公共MyView的(上下文的背景下){
超(上下文);
// TODO自动生成构造函数存根
BitmapFactory.Options BitmapFactoryOptionsbfo =新BitmapFactory.Options();
BitmapFactoryOptionsbfo.in preferredConfig = Bitmap.Config.RGB_565;
MYBITMAP = BitmapFactory.de codeResource(getResources(),R.drawable.face5,
BitmapFactoryOptionsbfo);
ImageWidth等= myBitmap.getWidth();
imageHeight = myBitmap.getHeight();
myFace =新FaceDetector.Face [numberOfFace]
myFaceDetect =新FaceDetector(ImageWidth等,imageHeight,numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(MYBITMAP,myFace);
}
@覆盖
保护无效的OnDraw(帆布油画){
// TODO自动生成方法存根
canvas.drawBitmap(MYBITMAP,0,0,NULL);
油漆myPaint =新的油漆();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
的for(int i = 0; I< numberOfFaceDetected;我++)
{
面对面= myFace [I]
的PointF myMidPoint =新的PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance();
canvas.drawRect(
(INT)(myMidPoint.x - myEyesDistance)
(INT)(myMidPoint.y - myEyesDistance)
(INT)(myMidPoint.x + myEyesDistance)
(INT)(myMidPoint.y + myEyesDistance)
myPaint);
}
}
}
}
您可以传递的setContentView()任何形式的观点,是布局的根视图。下面是一个动态生成的LinearLayout一个按钮,你的 MyView的
。
公共类例子扩展活动{
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的LinearLayout布局=新的LinearLayout(本);
//定义的LinearLayout的特点
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
//设置通用的布局参数
的LayoutParams PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
Button按钮=新的按钮(这一点);
button.setText(按钮!);
layout.addView(按钮,则params); //修改此
MyView的自定义=新的MyView的(这一点);
layout.addView(自定义,则params);当然//,这也
的setContentView(布局);
}
}
明白,如果你传递的setContentView()一个ViewGroup中只能添加子视图到你的根视图;像RelativeLayout的,LinearLayout中,等等。换句话说,你的不能做到这一点:
MyView的自定义=新的MyView的(这一点);
Button按钮=新的按钮(这一点);
button.setText(按钮!);
custom.addView(按钮);
//都能跟得上!方法addView()不能用于普通视图存在...
的setContentView(自定义);
此外,命名规则表明,在类名的每个单词应该有第一个字母大写。因此, MyView的
应该是 MyView的
,至少它使你的code更易于读取,其他程序员和编译器会突出显示与正确的颜色类的变量。
I know its possible to create activities by doing something like the code bellow, where the view is not set from xml file but like this: setContentView(new myView(this));
What i don't understand is how to use this code but still have the ability to customize it, for instance if i wanted to add a button to the code bellow, how would i do it, because i cant simply add one to an xml layout can i?
ANY GOOD ANSWERS TO THIS WILL VERY MUCH APPRECIATED thanks in advance!
package com.faceapp;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
public class FaceappActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(new myView(this));
}
private class myView extends View{
private int imageWidth, imageHeight;
private int numberOfFace = 5;
private FaceDetector myFaceDetect;
private FaceDetector.Face[] myFace;
float myEyesDistance;
int numberOfFaceDetected;
Bitmap myBitmap;
public myView(Context context) {
super(context);
// TODO Auto-generated constructor stub
BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,
BitmapFactoryOptionsbfo);
imageWidth = myBitmap.getWidth();
imageHeight = myBitmap.getHeight();
myFace = new FaceDetector.Face[numberOfFace];
myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(myBitmap, 0, 0, null);
Paint myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
for(int i=0; i < numberOfFaceDetected; i++)
{
Face face = myFace[i];
PointF myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance();
canvas.drawRect(
(int)(myMidPoint.x - myEyesDistance),
(int)(myMidPoint.y - myEyesDistance),
(int)(myMidPoint.x + myEyesDistance),
(int)(myMidPoint.y + myEyesDistance),
myPaint);
}
}
}
}
^^^^^^^^^^^^^^^ Answered
How to position the button and imageview? (Ideally using relative layout) The picture bellow shows you what i mean: (Ignore that the image is re-sized)
NEW CODE:
package com.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class TesttActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
Button button = new Button(this);
button.setText("Button!");
layout.addView(button);
myView custom = new myView(this);
layout.addView(custom);
setContentView(layout);
}
private class myView extends View{
private int imageWidth, imageHeight;
private int numberOfFace = 5;
private FaceDetector myFaceDetect;
private FaceDetector.Face[] myFace;
float myEyesDistance;
int numberOfFaceDetected;
Bitmap myBitmap;
public myView(Context context) {
super(context);
// TODO Auto-generated constructor stub
BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565;
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,
BitmapFactoryOptionsbfo);
imageWidth = myBitmap.getWidth();
imageHeight = myBitmap.getHeight();
myFace = new FaceDetector.Face[numberOfFace];
myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(myBitmap, 0, 0, null);
Paint myPaint = new Paint();
myPaint.setColor(Color.GREEN);
myPaint.setStyle(Paint.Style.STROKE);
myPaint.setStrokeWidth(3);
for(int i=0; i < numberOfFaceDetected; i++)
{
Face face = myFace[i];
PointF myMidPoint = new PointF();
face.getMidPoint(myMidPoint);
myEyesDistance = face.eyesDistance();
canvas.drawRect(
(int)(myMidPoint.x - myEyesDistance),
(int)(myMidPoint.y - myEyesDistance),
(int)(myMidPoint.x + myEyesDistance),
(int)(myMidPoint.y + myEyesDistance),
myPaint);
}
}
}
}
You can pass setContentView() any form of view, to be the root view of your layout. Below is a dynamically built LinearLayout with a Button and your myView
.
public class Example extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
// Define the LinearLayout's characteristics
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
// Set generic layout parameters
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button button = new Button(this);
button.setText("Button!");
layout.addView(button, params); // Modify this
myView custom = new myView(this);
layout.addView(custom, params); // Of course, this too
setContentView(layout);
}
}
Understand that you can only add child views to your root view if you pass setContentView() a ViewGroup; like RelativeLayout, LinearLayout, etc. In other words you cannot do this:
myView custom = new myView(this);
Button button = new Button(this);
button.setText("Button!");
custom.addView(button);
// Nope! Method "addView()" does not exist for a regular View...
setContentView(custom);
Also, naming convention suggests that each word in a class name should have the first letter capitalized. So myView
ought to be MyView
, at a minimum it makes your code easier to read for other programmers and the compiler will highlight your class variables with the correct color.
这篇关于如何创建无活动“的setContentView(R.layout.main)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!