我試圖讓一個獨立的代碼,以檢查是否會出現菜單,並出現在其他的方案,但在這段代碼,它不會出現,可能是什麼問題呢?爲什麼菜單選項不會出現
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dp = new Panel(this);
dp.setOnTouchListener(this);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.MITER);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeWidth(5);
//set framelayout for the head layout
fl = new FrameLayout(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
fl.addView(dp,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
this.getWindowManager().addView(fl , lp);
//cancel button
Button cancel = new Button(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 75);
params.bottomMargin =LayoutParams.MATCH_PARENT ;
params.gravity =Gravity.BOTTOM;
params.leftMargin =LayoutParams.MATCH_PARENT;
params.gravity =Gravity.BOTTOM;
cancel.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
fl.addView(cancel, params);
cancel.setOnClickListener(new ButtonClickHandlerCancel());
//save button
Button save = new Button(this);
FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 75);
params2.topMargin = LayoutParams.MATCH_PARENT;
params2.gravity =Gravity.BOTTOM;
params2.rightMargin = LayoutParams.MATCH_PARENT;
params2.bottomMargin = 75;
params2.gravity =Gravity.BOTTOM;
save.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
fl.addView(save, params2);
save.setOnClickListener(new ButtonClickHandlerSave());
//aesthetics for cancel button
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.cpressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.cunpressed));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.cpressed));
cancel.setBackgroundDrawable(states);
//aesthetics for save button
StateListDrawable states2 = new StateListDrawable();
states2.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.spressed));
states2.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.sunpressed));
states2.addState(new int[] { },
getResources().getDrawable(R.drawable.spressed));
save.setBackgroundDrawable(states2);
}
@Override public boolean onCreateOptionsMenu(Menu menu){ MenuInflater inflater = getMenuInflater(); inflater.inflate(R.layout.menu, menu); return true;
} @覆蓋 公共布爾onOptionsItemSelected(菜單項項){ 開關(item.getItemId()){ 情況R.id.eraser:
mPaint =新塗料( ); mPaint.setDither(true); mPaint.setColor(Color.TRANSPARENT); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.BUTT); mPaint.setStrokeWidth(5); 休息; case R.id.paint: mPaint = new Paint(); mPaint.setDither(true); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.MITER); mPaint.setStrokeCap(Paint.Cap.BUTT); mPaint.setStrokeWidth(5); 休息; } return true; }
//when cancel button is pressed
public class ButtonClickHandlerCancel implements View.OnClickListener {
public void onClick(View view) {
Intent intent = new Intent(MaleLayout.this,
BillysCrownMain.class);
startActivity(intent);
MaleLayout.this.finish();
}
}
//when save button is pressed
public class ButtonClickHandlerSave implements View.OnClickListener {
public void onClick(View view) {
dp.saveScreenshot();
}
}
//automatically created from panel
@Override
protected void onPause() {
super.onPause();
dp.pause();
}
//automatically created from panel
@Override
protected void onResume() {
super.onResume();
dp.resume();
}
//setting the path of paint based on finger touch
@Override
public boolean onTouch(View v, MotionEvent me) {
synchronized(pointsToDraw)
{
if(me.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(me.getX(), me.getY());
pointsToDraw.add(path);
}else if(me.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(me.getX(), me.getY());
}else if(me.getAction() == MotionEvent.ACTION_UP){
}
}
return true;
}
//the panel where the paint will be created
public class Panel extends SurfaceView implements Runnable{
public Panel(Context context) {
super(context);
holder = getHolder();
}
@Override
public void run() {
while(isItOk == true){
if(!holder.getSurface().isValid()){
continue;
}
holder.setFormat(PixelFormat.TRANSLUCENT);
c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
//the canvas where paint is drawn and background set
@Override
protected void onDraw(Canvas canvas) {
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.malehead);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, fl.getWidth(), fl.getHeight()-150, true);
c.drawBitmap(bMapScaled,0,0,null);
super.onDraw(canvas);
synchronized(pointsToDraw)
{
for (Path path : pointsToDraw)
{
canvas.drawPath(path, mPaint);
}
}
}
public void pause(){
isItOk = false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t = null;
}
public void resume(){
isItOk = true;
t = new Thread(this);
t.start();
}
//saving the file
public void saveScreenshot() {
if (ensureSDCardAccess()) {
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.malehead);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, fl.getWidth(), fl.getHeight()-150, true);
Canvas canvas = new Canvas(bMapScaled);
onDraw(canvas);
File file = new File(mScreenshotPath + "/" + System.currentTimeMillis() + ".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
Toast.makeText(MaleLayout.this, "Image saved at: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
bMapScaled.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.e("Panel", "FileNotFoundException", e);
} catch (IOException e) {
Log.e("Panel", "IOEception", e);
}
}
}
private boolean ensureSDCardAccess() {
File file = new File(mScreenshotPath);
if (file.exists()) {
return true;
} else if (file.mkdirs()) {
return true;
}
return false;
}
}