2013年1月1日
example.appflashlight
package com.example.appflashlight;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends Activity implements IColor {
public int widthPixels = 0;
public int heightPixels = 0;
RelativeLayout layout;
RelativeLayout.LayoutParams params;
// MainView mainView;
ImageButtonA imageButtonA;
ColorDrawView colorDrawView;
SharedPreferences spref;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// setBackgroundColor(Color.argb(255, 50, 50, 50));
spref = getPreferences(MODE_PRIVATE);
int IntFLAGA = spref.getInt("KEY_FLAGA", 0);
Log.e("MainActivity", "IntFLAGA " + IntFLAGA);
ImageOne.setImageOneflagA(IntFLAGA);
int IntColor = spref.getInt("KEY_COLOR", -14803201);
Log.e("MainActivity", "IntColor " + IntColor);
PaintOne.setPaintOne(IntColor);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
widthPixels = dm.widthPixels;
heightPixels = dm.heightPixels;
layout = new RelativeLayout(this);
layout.setId(20001);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
MainActivityView mainActivityView = new MainActivityView(this);
mainActivityView.setId(20002);
mainActivityView.setLayoutParams(params);
layout.addView(mainActivityView);
colorDrawView = new ColorDrawView(this);
colorDrawView.setId(859199);
params = new RelativeLayout.LayoutParams(colorDrawView.width,
colorDrawView.height);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.setMargins(90, 120, 0, 0);
colorDrawView.setLayoutParams(params);
layout.addView(colorDrawView);
Log.e("MainActivity", "MainActivity onCreate");
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// params.setMargins(20, 20, 20, 20);
// params.addRule(RelativeLayout.ALIGN_RIGHT,layout.getId());
ButtonBmpA buttonBmpA = new ButtonBmpA(this);
buttonBmpA.setLayoutParams(params);
layout.addView(buttonBmpA);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_TOP, buttonBmpA.getId());
params.addRule(RelativeLayout.RIGHT_OF, buttonBmpA.getId());
// params.setMargins(20, 20, 20, 20);
ButtonBmpB buttonBmpB = new ButtonBmpB(this);
buttonBmpB.setLayoutParams(params);
layout.addView(buttonBmpB);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.setMargins(90, 120, 0, 0);
imageButtonA = new ImageButtonA(this);
imageButtonA.setLayoutParams(params);
layout.addView(imageButtonA);
setContentView(layout);
}
public void goSecondActivity() {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
this.startActivity(intent);
}
public void goThirdActivity() {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ThirdActivity.class);
this.startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
// Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onDestroy()");
}
@Override
protected void onPause() {
super.onPause();
// Toast.makeText(this, "OnPause()", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onPause()");
}
@Override
protected void onRestart() {
super.onRestart();
Log.e("MainActivity", "onRestart()");
// Toast.makeText(this, "OnRestart() WELCOME BACK", Toast.LENGTH_SHORT)
// .show();
}
@Override
protected void onResume() {
super.onResume();
// Toast.makeText(this, "OnResume()", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onResume()");
imageButtonA.setBackgroundResource(ImageOne.getflagShow());
colorDrawView.setmPaintColor();
}
@Override
protected void onStart() {
super.onStart();
// Toast.makeText(this, "OnStart()", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onStart()");
}
@Override
protected void onStop() {
super.onStop();
// Toast.makeText(this, "OnStop()", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onStop()");
SharedPreferences.Editor editor = spref.edit();
editor.clear();
editor.putInt("KEY_FLAGA", ImageOne.getFlagA());
editor.putInt("KEY_COLOR", PaintOne.getPaintColor());
editor.commit();
if (CameraOne.getCamera() != null) {
CameraOne.setCamera();
ImageOne.setImageOneflagB();
}
}
}
class MainActivityView extends View {
private ShapeDrawable mDrawable;
private Paint mPaint;
private int width = 0;
private int height = 0;
public MainActivityView(Context context) {
super(context);
// TODO Auto-generated constructor stub
MainActivity mainActivity = (MainActivity) context;
height = mainActivity.heightPixels;
width = mainActivity.widthPixels;
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.setBounds(0, 0, width, height);
mPaint = mDrawable.getPaint();
mPaint.setColor(Color.argb(255, 0, 0, 0));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
mDrawable.draw(canvas);
}
}
class ColorDrawView extends View {
public Bitmap bollBitmap;
private ShapeDrawable mDrawable;
private Paint mPaint;
public int width = 0;
public int height = 0;
private Canvas canvas;
public ColorDrawView(Context context) {
super(context);
// TODO Auto-generated constructor stub
MainActivity mainActivity = (MainActivity) context;
bollBitmap = BitmapFactory.decodeResource(mainActivity.getResources(),
R.drawable.image_0);
width = bollBitmap.getWidth();
height = bollBitmap.getHeight();
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.setBounds(0, 0, width, height);
mPaint = mDrawable.getPaint();
mPaint.setColor(PaintOne.getPaintColor());
mPaint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.INNER));
// mPaint=PaintOne.getPaintOne();
}
public void setmPaintColor() {
mPaint.setColor(PaintOne.getPaintColor());
invalidate(); // 每呼叫一次就會重新繪圖onDraw()
}
@Override
protected void onDraw(Canvas _canvas) {
// TODO Auto-generated method stub
canvas = _canvas;
canvas.drawBitmap(bollBitmap, 0, 0, mPaint);
mDrawable.draw(canvas);
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言