当前位置:  编程技术>移动开发
本页文章导读:
    ▪游戏菜单MenuList种        游戏菜单MenuList类 MenuList类继承了高级用户界面的List类,由于菜单属于IMPLICIT,因此需要监听各个选项的选定事件,并且还实现了CommandListener接口。 ......
    ▪ 色彩选择器        颜色选择器   ColorPick.java public class ColorPickerPreference extends DialogPreference { private int mInitialColor; private int mCurrentColor; private ColorPickerView colorPickerView; public interface OnColorChangedListener { void colorC.........
    ▪ 设立震动       设置震动 < uses-permission android:name="android.permission.VIBRATE" /> java代码: public class helloWorld extends Activity {   public void onCreate(Bundle savedInstanceState)   {   super.onCreate(savedInstanceState);  .........

[1]游戏菜单MenuList种
    来源: 互联网  发布时间: 2014-02-18
游戏菜单MenuList类
MenuList类继承了高级用户界面的List类,由于菜单属于IMPLICIT,因此需要监听各个选项的选定事件,并且还实现了CommandListener接口。

    
[2] 色彩选择器
    来源: 互联网  发布时间: 2014-02-18
颜色选择器



 

ColorPick.java

public class ColorPickerPreference extends DialogPreference {
	private int mInitialColor;
	private int mCurrentColor;
	private ColorPickerView colorPickerView;

	public interface OnColorChangedListener {
		void colorChanged(int color);
	}

	public ColorPickerPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ColorPickerPreference(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		if (positiveResult) {
			mCurrentColor = colorPickerView.getColor();
			System.out.println("当前选中的颜色:" + mCurrentColor);
			// 将颜色保存到SharedPreferences
			SharedPreferences sp = getContext().getApplicationContext()
					.getSharedPreferences("mycolor", Context.MODE_PRIVATE); // 获得Preferences
			Editor editor = sp.edit();
			editor.putInt(getKey(), mCurrentColor);
			editor.commit();
			callChangeListener(new Integer(mCurrentColor));
		}
	}

	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		super.onPrepareDialogBuilder(builder);

		OnColorChangedListener onColorChangedListener = new OnColorChangedListener() {
			public void colorChanged(int color) {
				mCurrentColor = color;
				onDialogClosed(true);
				getDialog().dismiss();
			}
		};

		SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
		mInitialColor = prefs.getInt(getKey(), Color.LTGRAY);
		LinearLayout layout = new LinearLayout(getContext());
		layout.setPadding(20, 20, 20, 20);
		layout.setOrientation(LinearLayout.VERTICAL);
		colorPickerView = new ColorPickerView(getContext(),
				onColorChangedListener, mInitialColor);
		LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		params1.gravity = Gravity.CENTER;
		colorPickerView.setLayoutParams(params1);
		layout.addView(this.colorPickerView);
		layout.setId(android.R.id.widget_frame);
		builder.setView(layout);
	}

	/**
	 * 颜色选择面板
	 * 
	 * @author lilin
	 * @date 2011-9-15 下午03:54:27
	 * @ClassName: ColorPickerView
	 * @Description: TODO
	 */
	private static class ColorPickerView extends View {
		private Paint mPaint;
		private Paint mCenterPaint;
		private Paint mHSVPaint;
		private final int[] mColors;
		private int[] mHSVColors;
		private boolean mRedrawHSV;
		private OnColorChangedListener mListener;

		ColorPickerView(Context c, OnColorChangedListener l, int color) {
			super(c);
			mListener = l;
			mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,
					0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
			Shader s = new SweepGradient(0, 0, mColors, null);

			mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			mPaint.setShader(s);
			mPaint.setStyle(Paint.Style.STROKE);
			mPaint.setStrokeWidth(55);

			mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			mCenterPaint.setColor(color);
			mCenterPaint.setStrokeWidth(5);

			mHSVColors = new int[] { 0xFF000000, color, 0xFFFFFFFF };

			mHSVPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			mHSVPaint.setStrokeWidth(10);

			mRedrawHSV = true;
		}

		private boolean mTrackingCenter;
		private boolean mHighlightCenter;

		public int getColor() {
			return mCenterPaint.getColor();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;

			canvas.translate(CENTER_X, CENTER_X);
			int c = mCenterPaint.getColor();

			if (mRedrawHSV) {
				mHSVColors[1] = c;
				mHSVPaint.setShader(new LinearGradient(-100, 0, 100, 0,
						mHSVColors, null, Shader.TileMode.CLAMP));
			}

			canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
			canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);
			canvas.drawRect(new RectF(-100, 130, 100, 110), mHSVPaint);

			if (mTrackingCenter) {
				mCenterPaint.setStyle(Paint.Style.STROKE);

				if (mHighlightCenter) {
					mCenterPaint.setAlpha(0xFF);
				} else {
					mCenterPaint.setAlpha(0x80);
				}
				canvas.drawCircle(0, 0, CENTER_RADIUS
						+ mCenterPaint.getStrokeWidth(), mCenterPaint);

				mCenterPaint.setStyle(Paint.Style.FILL);
				mCenterPaint.setColor(c);
			}

			mRedrawHSV = true;
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			setMeasuredDimension(CENTER_X * 2, (CENTER_Y + 25) * 2);
		}

		private static final int CENTER_X = 100;
		private static final int CENTER_Y = 100;
		private static final int CENTER_RADIUS = 30;

		private int ave(int s, int d, float p) {
			return s + java.lang.Math.round(p * (d - s));
		}

		private int interpColor(int colors[], float unit) {
			if (unit <= 0) {
				return colors[0];
			}
			if (unit >= 1) {
				return colors[colors.length - 1];
			}

			float p = unit * (colors.length - 1);
			int i = (int) p;
			p -= i;

			// now p is just the fractional part [0...1) and i is the index
			int c0 = colors[i];
			int c1 = colors[i + 1];
			int a = ave(Color.alpha(c0), Color.alpha(c1), p);
			int r = ave(Color.red(c0), Color.red(c1), p);
			int g = ave(Color.green(c0), Color.green(c1), p);
			int b = ave(Color.blue(c0), Color.blue(c1), p);

			return Color.argb(a, r, g, b);
		}

		private static final float PI = 3.1415926f;

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			float x = event.getX() - CENTER_X;
			float y = event.getY() - CENTER_Y;
			boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS;

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mTrackingCenter = inCenter;
				if (inCenter) {
					mHighlightCenter = true;
					invalidate();
					break;
				}
			case MotionEvent.ACTION_MOVE:
				if (mTrackingCenter) {
					if (mHighlightCenter != inCenter) {
						mHighlightCenter = inCenter;
						invalidate();
					}
				} else if ((x >= -100 & x <= 100) && (y <= 130 && y >= 110)) // see
				// if
				// we're
				// in
				// the
				// hsv
				// slider
				{
					int a, r, g, b, c0, c1;
					float p;

					// set the center paint to this color
					if (x < 0) {
						c0 = mHSVColors[0];
						c1 = mHSVColors[1];
						p = (x + 100) / 100;
					} else {
						c0 = mHSVColors[1];
						c1 = mHSVColors[2];
						p = x / 100;
					}

					a = ave(Color.alpha(c0), Color.alpha(c1), p);
					r = ave(Color.red(c0), Color.red(c1), p);
					g = ave(Color.green(c0), Color.green(c1), p);
					b = ave(Color.blue(c0), Color.blue(c1), p);

					mCenterPaint.setColor(Color.argb(a, r, g, b));

					mRedrawHSV = false;
					invalidate();
				} else {
					float angle = (float) java.lang.Math.atan2(y, x);
					// need to turn angle [-PI ... PI] into unit [0....1]
					float unit = angle / (2 * PI);
					if (unit < 0) {
						unit += 1;
					}
					mCenterPaint.setColor(interpColor(mColors, unit));
					invalidate();
				}
				break;
			case MotionEvent.ACTION_UP:
				if (mTrackingCenter) {
					if (inCenter) {
						mListener.colorChanged(mCenterPaint.getColor());
					}
					mTrackingCenter = false; // so we draw w/o halo
					invalidate();
				}
				break;
			}
			return true;
		}
	}
}

 

Main.java

public class Main extends PreferenceActivity {
	Button button;
	SharedPreferences sp;
	ListView listView;
	LinearLayout linearLayout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preference);
		setContentView(R.layout.main);
		button = (Button) findViewById(R.id.button);
		listView = new ListView(this);
		linearLayout = (LinearLayout) findViewById(R.id.line11);
		getColor();

	}

	public void getColor() {
		sp = getApplicationContext().getSharedPreferences("mycolor",
				Context.MODE_PRIVATE); // 获得Preferences
		int color = sp.getInt("mycolor", 0);
		String string = String.valueOf(color);
		button.setText(string);
		button.setTextColor(color);
		listView.setBackgroundColor(color);
		linearLayout.setBackgroundColor(color);
	}


}

 


    
[3] 设立震动
    来源: 互联网  发布时间: 2014-02-18
设置震动
< uses-permission android:name="android.permission.VIBRATE" />

java代码:
public class helloWorld extends Activity {
  public void onCreate(Bundle savedInstanceState)
  {
  super.onCreate(savedInstanceState);
  Vibrator vVi = (Vibrator)this.getApplication().getSystemService(Service.VIBRATOR_SERVICE);
  // 震动0.5 秒
  vVi.vibrate( 500 );
  //{200,200,1000}这样的mode表示的是等待0.2秒, 振动0.2秒, 等待1秒
  -1表示不重复, 如果不是-1, 比如改成1, 表示从前面这个long数组的下标为1的元素开始重复.
  ) vVi.vibrate( new long[]{200, 200, 1000}, -1 );
  }
  }

    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3