LCD屏幕测试灰阶

来源:互联网 发布:固态用软件优化 编辑:程序博客网 时间:2024/04/30 13:39

DrawPaintMainActivity   Main

package com.gigaset.lcd;


import java.util.ArrayList;
import java.util.List;


import com.example.gigaset.R;
import com.example.gigaset.R.id;
import com.example.gigaset.R.layout;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;


public class DrawPaintMainActivity extends Activity {


protected static final String tag = "DrawPaint";
private DrawCG mDrawCG;
private ImageView imgView;
private EditText r1 = null;
private EditText g1 = null;
private EditText b1 = null;
private Button paintButton = null;
private Button blackButton = null;
private Button whiteButton = null;
private ImageButton Left = null;
private ImageButton Right = null;
private EditText huiJieText = null;


private LinearLayout toplinear;
private LinearLayout bottomLinear;
private LinearLayout choseLayout;
private LinearLayout imageLayout;
private long exitTime = 0;
private static PowerManager pManager = null;
public static PowerManager.WakeLock wLock = null;
private KeyguardManager kManager = null;


@SuppressWarnings("deprecation")
public static KeyguardManager.KeyguardLock kLock = null;


private Spinner spinner = null;
private List<String> list = new ArrayList<String>();
int screenWidth;
int screenHeight;
boolean FlagOne;
boolean FlagTwo;
boolean FlagThree;


int r;


public void init() {
// 获取手机窗口的大小
WindowManager wm = getWindowManager();
Display display = wm.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Log.e(tag, "screenwidth------->" + screenWidth);
Log.e(tag, "screenheight----->" + screenHeight);


imgView = (ImageView) findViewById(R.id.imgView);
mDrawCG = new DrawCG(DrawPaintMainActivity.this, screenWidth,
screenHeight);
this.r1 = (EditText) findViewById(R.id.r1);
this.g1 = (EditText) findViewById(R.id.g1);
this.b1 = (EditText) findViewById(R.id.b1);


this.paintButton = (Button) findViewById(R.id.paint);
this.toplinear = (LinearLayout) findViewById(R.id.toplinearlayout);
this.bottomLinear = (LinearLayout) findViewById(R.id.bottomlinearlayout);
this.choseLayout = (LinearLayout) findViewById(R.id.sejielayout);
this.imageLayout = (LinearLayout) findViewById(R.id.imagelayout);


this.blackButton = (Button) findViewById(R.id.black);
this.whiteButton = (Button) findViewById(R.id.white);
this.spinner = (Spinner) findViewById(R.id.spinner1);


this.Left = (ImageButton) findViewById(R.id.imageButtonleft);
this.Right = (ImageButton) findViewById(R.id.imageButtonright);
this.huiJieText = (EditText) findViewById(R.id.huijieedit);


list.add("0");
list.add("64灰阶");
list.add("256灰阶");
list.add("外围白,中心黑");
list.add("外围白,中心灰");
list.add("外围灰,中心白");
list.add("外围灰,中心黑");


}


private void hidden() {
// TODO Auto-generated method stub
toplinear.setVisibility(View.GONE);
bottomLinear.setVisibility(View.GONE);
choseLayout.setVisibility(View.GONE);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}


public void getNoLock() {
// TODO Auto-generated method stub
Log.e(tag, "wakeup  and getKeyGuard");
wakeUp();
getKeyGuard();
}


@SuppressWarnings("deprecation")
private void getKeyGuard() {
// TODO Auto-generated method stub
kManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (kManager.inKeyguardRestrictedInputMode()) {
kLock = kManager.newKeyguardLock(tag);
kLock.disableKeyguard();
} else {
}
}


@SuppressWarnings("deprecation")
private void wakeUp() {
// TODO Auto-generated method stub
pManager = (PowerManager) getSystemService(POWER_SERVICE);


wLock = pManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE | PowerManager.FULL_WAKE_LOCK,
"wakeLock");


wLock.acquire();


}


@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 无标题设置
requestWindowFeature(Window.FEATURE_NO_TITLE);


// 定义全屏参数
int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
// 获得当前窗体对象
Window window = DrawPaintMainActivity.this.getWindow();
// 设置当前窗体为全屏显示
window.setFlags(flag, flag); 
// //启动log日志服务
// startService(new Intent(getApplicationContext(), LogService.class));
getNoLock();
/* 全屏 * */
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);


setContentView(R.layout.test);


init();
// adpater对象
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, list);
arrayAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 设置显示的数据
spinner.setAdapter(arrayAdapter);
Left.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (FlagOne) {
int k = Integer.parseInt(huiJieText.getText().toString());
k--;
if (k <1) {
k =64;
// Log.e(tag, "当超出范围时间K的值是:" + k+", 是一个无效值");
Toast.makeText(getApplicationContext(),
"64灰阶测试----请输入1-64之间的数值", Toast.LENGTH_SHORT)
.show();
}else {

huiJieText.setText(String.valueOf(k));
hidden();
imgView.setImageBitmap(mDrawCG.Onehuijie(k, k, k));
Toast.makeText(getApplicationContext(),
"当前执行的是64灰阶中的第" + k + "灰阶", Toast.LENGTH_SHORT)
.show();
}


}
if (FlagTwo) {
int k = Integer.parseInt(huiJieText.getText().toString());
k--;


if (k <= 0) {
k = 0;
}
Log.e(tag, "256灰阶K的值:--->" + k);
huiJieText.setText(String.valueOf(k));
hidden();
imgView.setImageBitmap(mDrawCG.paintBitmap(k, k, k));
// FlagTwo = false ;
Toast.makeText(getApplicationContext(),
"当前执行的是255灰阶中的第" + k + "灰阶", Toast.LENGTH_SHORT)
.show();


}


}
});
Right.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (FlagOne) {
int k = Integer.parseInt(huiJieText.getText().toString());
k++;


if (k > 64) {
k = 64;
Toast.makeText(getApplicationContext(), "请输入1-64之间的数值",
Toast.LENGTH_SHORT).show();


}


Log.e(tag, "64灰阶K的值:--->" + k);
huiJieText.setText(String.valueOf(k));
hidden();
imgView.setImageBitmap(mDrawCG.Onehuijie(k, k, k));
Toast.makeText(getApplicationContext(),
"当前执行的是64灰阶中的第" + k + "灰阶", Toast.LENGTH_SHORT)
.show();


}


if (FlagTwo) {
int k = Integer.parseInt(huiJieText.getText().toString());
k++;


if (k >= 254) {
k = 255;
}
Log.e(tag, "256灰阶K的值:--->" + k);
huiJieText.setText(String.valueOf(k));
hidden();
imgView.setImageBitmap(mDrawCG.paintBitmap(k, k, k));
// FlagTwo = false ;
Toast.makeText(getApplicationContext(),
"当前执行的是255灰阶中的第" + k + "灰阶", Toast.LENGTH_SHORT)
.show();
}


}
});


spinner.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
Log.e(tag, "当前的选择是 :" + arrayAdapter.getItem(position));
arg0.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(),
"选择的灰阶" + id + "内容是:" + arrayAdapter.getItem(position),
Toast.LENGTH_SHORT).show();


if (id == 1) {
// 64灰阶
Log.e(tag, "64huijie选择");
FlagOne = true;
FlagTwo = false;
huiJieText.setText("0");


}
if (id == 2) {
// 256灰阶
Log.e(tag, "256huijie选择");
FlagTwo = true;
FlagOne = false;
huiJieText.setText("0");
// hidden();
// int k = 0;
// imgView.setImageBitmap(mDrawCG.paintBitmap(k, k, k));
}


if (id == 3) {
// 外围白,中心黑
Log.e(tag, "外围白,中心黑chose");
hidden();
imgView.setImageBitmap(mDrawCG.BB(screenWidth,
screenHeight, screenWidth, screenHeight));


}


if (id == 4) {
// 外围白,中心灰色
Log.e(tag, "外围白,中心灰色chose");
hidden();
imgView.setImageBitmap(mDrawCG.Bh(screenWidth,
screenHeight, screenWidth, screenHeight));


}
if (id == 5) {
// 外围白,中心灰色
Log.e(tag, "外围灰色,中心白色chose");
hidden();
imgView.setImageBitmap(mDrawCG.BJ(screenWidth,
screenHeight, screenWidth, screenHeight));


}
if (id == 6) {
// 外围白,中心灰色
Log.e(tag, "外围灰色,中心 黑色chose");
hidden();
imgView.setImageBitmap(mDrawCG.BK(screenWidth,
screenHeight, screenWidth, screenHeight));


}


}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub


}
});


paintButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* 这里缺少对三个值是否是空进行判断, 如果出现空,可能会出现报错的现象 *
*/


r = Integer.parseInt(r1.getText().toString());
int g = Integer.parseInt(g1.getText().toString());
int b = Integer.parseInt(b1.getText().toString());
Log.e(tag, "r的值" + r);
Log.e(tag, "gi的值" + g);
Log.e(tag, "bi的值" + b);


if ((r > 255 | r < 0) | (g > 255 | g < 0) | (b > 255 | b < 0)) {
Toast.makeText(DrawPaintMainActivity.this,
"该填充色板是255色阶,请输入0~255之间的数值", Toast.LENGTH_SHORT)
.show();
} else {
hidden();
// 执行画图
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
imgView.setImageBitmap(mDrawCG.paintBitmap(r, g, b));
}
}
});


blackButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hidden();
imgView.setImageBitmap(mDrawCG.drawBlack());
}
});


whiteButton.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
hidden();
imgView.setImageBitmap(mDrawCG.drawWhite());
}
});
// imageLayout.setOnTouchListener(this);
}


// 按钮事件
public void doCreateLine(View view) {
hidden();
imgView.setImageBitmap(mDrawCG.drawRed());
}


public void doCreateTriangle(View view) {
hidden();
imgView.setImageBitmap(mDrawCG.drawBlue());
}


public void doCreateRect(View view) {
hidden();
imgView.setImageBitmap(mDrawCG.drawGreen());
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
/** 这里增加back 的作用 */


if (keyCode == KeyEvent.KEYCODE_BACK) {


exit();
return false;
}
return super.onKeyDown(keyCode, event);


}


private void exit() {
// TODO Auto-generated method stub
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "快速双击返回两次可退出",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
toplinear.setVisibility(View.VISIBLE);
bottomLinear.setVisibility(View.VISIBLE);
choseLayout.setVisibility(View.VISIBLE);
} else {
finish();
System.exit(0);
}
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
wLock.release();


super.onDestroy();
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}


}


XML 布局 文件 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:id="@+id/toplinearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/R"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/R" >
        </TextView>


        <EditText
            android:id="@+id/r1"
            android:layout_width="42dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.28"
            android:ems="255"
            android:inputType="number" >


            <requestFocus />
        </EditText>


        <TextView
            android:id="@+id/G"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/G" />


        <EditText
            android:id="@+id/g1"
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.23"
            android:ems="255"
            android:inputType="number" />


        <TextView
            android:id="@+id/B"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/B" />


        <EditText
            android:id="@+id/b1"
            android:layout_width="43dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.32"
            android:ems="255"
            android:inputType="number" />


        <Button
            android:id="@+id/paint"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:text="@string/paint" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/sejielayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="114dp"
            android:layout_height="match_parent"
            android:layout_gravity="clip_horizontal" />


        <ImageButton
            android:id="@+id/imageButtonleft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_media_rew" />


        <EditText
            android:id="@+id/huijieedit"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />


        <ImageButton
            android:id="@+id/imageButtonright"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_media_ff" />


    </LinearLayout>


    <LinearLayout
        android:id="@+id/imagelayout"
        android:layout_width="match_parent"
        android:layout_height="274dp"
        android:layout_weight="0.1"
        android:background="#FFFFFFFF" >


        <ImageView
            android:id="@+id/imgView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY" />
    </LinearLayout>


    <LinearLayout
        android:id="@+id/bottomlinearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_horizontal"
            android:layout_weight="0.03"
            android:onClick="doCreateLine"
            android:text="@string/R" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.03"
            android:layout_gravity="fill_horizontal"
            android:onClick="doCreateTriangle"
            android:text="@string/B" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.03"
            android:layout_gravity="fill_horizontal"
            android:onClick="doCreateRect"
            android:text="@string/G" />


        <Button
            android:id="@+id/black"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.03"
              android:layout_gravity="fill_horizontal"
            android:text="@string/BL" />


        <Button
            android:id="@+id/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.03"
             android:layout_gravity="fill_horizontal"
            android:text="@string/W" />
    </LinearLayout>


</LinearLayout>


调用的VIEW 类型


package com.gigaset.lcd;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.Log;
import android.view.View;


class DrawCG extends View {


private static final String tag = "DrawCG";
private Paint paint;
private Canvas canv;
private Bitmap mBitmap;


public DrawCG(Context context, int width, int height) {
super(context);
// 声明画笔
paint = new Paint();
// 设置颜色
paint.setColor(Color.RED);
// 设置抗锯齿
paint.setAntiAlias(true);
// 设置线宽
paint.setStrokeWidth(3);
// 设置非填充
paint.setStyle(Style.STROKE);
// 声明位图
mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
// 声明画布
canv = new Canvas(mBitmap);
}


@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, null);
// super.onDraw(canvas);
}


/**
* RED

* @return
*/
public Bitmap drawRed() {
// canv.drawLine(200, 50, 600, 50, paint);
// canv.drawARGB(64,3, 3, 3);
canv.drawColor(Color.RED);


return mBitmap;


}


/**
* blue

* @return
*/
public Bitmap drawBlue() {
canv.drawColor(Color.BLUE);
return mBitmap;
}


/**
* green

* @return
*/
public Bitmap drawGreen() {


// canv.drawRect(new Rect(150, 150, 500, 500), paint);
canv.drawColor(Color.GREEN);


return mBitmap;
}


/*
* black
*/
public Bitmap drawBlack() {


// canv.drawRect(new Rect(150, 150, 500, 500), paint);
canv.drawColor(Color.BLACK);


return mBitmap;
}


/*
* white *
*/
public Bitmap drawWhite() {


// canv.drawRect(new Rect(150, 150, 500, 500), paint);
canv.drawColor(Color.WHITE);


return mBitmap;
}


/*
* 255灰阶 *
*/
public Bitmap paintBitmap(int r, int g, int b) {
Log.e(tag, "调用画图的方法,这里传入参数, r-->" + r + "| g--->" + g + "|b--->" + b);
canv.drawRGB(r, g, b);
return mBitmap;


}


/*
* 64灰阶 *
*/
public Bitmap Onehuijie(int r, int g, int b) {
Log.e(tag, "64  4*灰阶调用");
// canv.drawARGB(32, 4 * r - 1, 4 * g - 1, 4 * b - 1);


if ((r == 64) | (g == 64) |( b == 64)) {
canv.drawRGB(255, 255, 255);
Log.e(tag, "r ,g ,b 的值都是 255 , 255, 255");
}else {
canv.drawRGB(4 * r , 4 * g , 4 * b);
Log.e(tag, "64灰阶计算执行,(4*r):"+(4*r)+"(4*g):"+(4*g)+"(4*b):"+(4*b));
}

return mBitmap;
}


/*
* 外围白,中心黑*
*/
public Bitmap BB(float left, float top, float right, float bottom) {
Log.e(tag, "外围白,中心黑class");
canv.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.BLACK); // 设置画笔颜色
paint.setStyle(Style.FILL);// 设置填充样式
paint.setStrokeWidth(15);// 设置画笔宽度
// canv.drawRect(135, 444, 1000, 1332, paint);
// canv.drawRect(left / 8, top / 4, right * 7 / 8, bottom * 3 / 4,
// paint);// 直接构造
canv.drawRect(left / 4, top / 4, right * 3 / 4, bottom * 3 / 4, paint);// 直接构造
return mBitmap;
}


public Bitmap Bh(float left, float top, float right, float bottom) {
Log.e(tag, "外围白,中心huiclass");
canv.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setColor(Color.rgb(128, 128, 128)); // 设置画笔颜色
paint.setStyle(Style.FILL);// 设置填充样式
paint.setStrokeWidth(15);// 设置画笔宽度


canv.drawRect(left / 4, top / 4, right * 3 / 4, bottom * 3 / 4, paint);// 直接构造
return mBitmap;
}


public Bitmap BJ(float left, float top, float right, float bottom) {
Log.e(tag, "外围灰色,中心白class");
canv.drawColor(Color.rgb(128, 128, 128));
Paint paint = new Paint();
paint.setColor(Color.WHITE); // 设置画笔颜色
paint.setStyle(Style.FILL);// 设置填充样式
paint.setStrokeWidth(15);// 设置画笔宽度


canv.drawRect(left / 4, top / 4, right * 3 / 4, bottom * 3 / 4, paint);// 直接构造
return mBitmap;
}


public Bitmap BK(float left, float top, float right, float bottom) {
Log.e(tag, "外围灰色,中心blackclass");
canv.drawColor(Color.rgb(128, 128, 128));
Paint paint = new Paint();
paint.setColor(Color.BLACK); // 设置画笔颜色
paint.setStyle(Style.FILL);// 设置填充样式
paint.setStrokeWidth(15);// 设置画笔宽度


canv.drawRect(left / 4, top / 4, right * 3 / 4, bottom * 3 / 4, paint);// 直接构造
return mBitmap;
}


}






0 0
原创粉丝点击