TextView法改变字体颜色 10C

用TabLayout+ViewPager,实现了导航,pager使用了两个Fragment。在Fragment实现布局的时候添加了多个TextView,在预览状态可以看到字体颜色的修改是成功的,但是当程序运行起来后发现,页面无任何文字。然后给TextView添加背景色,运行发现字体颜色是白色的,之前因为背景也是白色,没显示出来,之后有尝试修改,发现无论怎么修改都**无法改变字体颜色,只能为白色**。
最后发现,在代码中可以设置文字的颜色,但是这样的话很不方便,不知道哪位朋友遇到过,或者知道原因,麻烦能指导下,谢谢

4个回答

你把fragment的view设置个背景色试试

sinat_26212595
星光下的阴影 fragment的view设置了背景色,但是我需要改变的是字体颜色,而且多个TextView的字体颜色还不一样。谢谢指导
接近 4 年之前 回复

可以吧fragment中的view设个背景

sinat_26212595
星光下的阴影 fragment的view设置了背景色,但是我需要改变的是字体颜色,而且多个TextView的字体颜色还不一样。谢谢指导
接近 4 年之前 回复

颜色是例如#ff0000的话,改成#ffff0000试下,看是不是这个问题,没有透明度

你可以改变Actiivty 主题 改成
android:theme="@android:style/Theme.Black" 默认背景可能就变成黑色了,字体白色就要吧看见了。

sinat_26212595
星光下的阴影 确实是设置的主题的原因,android:theme="@style/Theme.AppCompat.NoActionBar"用的这个主题,不知道因为啥
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
为什么没有法改变TextView的字体颜色?
package com.like.duoroulianmeng; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.TextView; public class Buttom_meun extends FragmentActivity { TextView[] textView;// 定义一个数组装底部菜单的 TextView的ID ImageView[] imageView; int index;// 点击后视图下标 int currentTabIndex;// 当前视图下标 private FragmentManager fragmentManager; Fragment[] mFragments; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_buttom_meun); mFragments = new Fragment[5]; fragmentManager = this.getSupportFragmentManager(); mFragments[0] = fragmentManager.findFragmentById(R.id.shouye_fragment); mFragments[1] = fragmentManager.findFragmentById(R.id.xianshitemai_fragment); mFragments[2] = fragmentManager.findFragmentById(R.id.shequ_fragment); mFragments[3] = fragmentManager.findFragmentById(R.id.gouwuche_fragment); mFragments[4] = fragmentManager.findFragmentById(R.id.wode_fragment); fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]) .hide(mFragments[3]).hide(mFragments[4]); fragmentTransaction.show(mFragments[0]).commit(); textView = new TextView[5]; textView[0] = (TextView) findViewById(R.id.tv_shouye); textView[1] = (TextView) findViewById(R.id.tv_xianshitemai); textView[2] = (TextView) findViewById(R.id.tv_shequ); textView[3] = (TextView) findViewById(R.id.tv_gouwuche); textView[4] = (TextView) findViewById(R.id.tv_wode); textView[0].setTextColor(0xFFFF5001); imageView = new ImageView[5]; imageView[0] = (ImageView) findViewById(R.id.imageview4); imageView[1] = (ImageView) findViewById(R.id.imageview5); imageView[2] = (ImageView) findViewById(R.id.imageview6); imageView[3] = (ImageView) findViewById(R.id.imageview7); imageView[4] = (ImageView) findViewById(R.id.imageview8); System.out.println(imageView[1]+"11111"); imageView[0].setSelected(true); } public void onTabClicked(View view) { fragmentTransaction = fragmentManager.beginTransaction() .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]) .hide(mFragments[3]).hide(mFragments[4]); switch (view.getId()) { case R.id.shouye: index = 0; break; case R.id.xianshitemai: index = 1; break; case R.id.shequ: index = 2; break; case R.id.gouwuche: index = 3; break; case R.id.wode: index = 4; break; } textView[currentTabIndex].setTextColor(0xFF9A9A9A); imageView[currentTabIndex].setSelected(false); textView[index].setTextColor(0xFFFF5001); imageView[index].setSelected(true); System.out.println(imageView[index]+"222222"); fragmentTransaction.show(mFragments[index]).commit(); currentTabIndex = index; } }
在一个textview中批量修改文本颜色
能不能实现使用一个`textview`修改多个文本字符串的文本颜色,我用`Html.fromHtml`尝试失败: textViewPrevChat.append("\n"+Html.fromHtml("<b>Bold string </b>")+somestringhere); 运行之后,所有的字体加粗,但是在 **<p>** 中的颜色属性没有改变。
如何给TextView的部分文本添加颜色
我想改变textview中某些文字的颜色。例如,搜索结果的颜色。 我试图使用 ANSI 颜色控件: text.setText("\u001B31;1m" + "someText"); 但是没有实现所要的功能。谁有好的建议呢?
Android方面的问题,不使用TextView,怎样直接在string.xml里修改字体颜色?
就像这样,加粗没问题,但是颜色不知道格式是怎样的,请问大家能实现吗? ![图片说明](https://img-ask.csdn.net/upload/201601/29/1454081916_891267.png)
改变 texView 的字体颜色
我使用下面的textView方法来改变字体颜色。使用什么格式来指定颜色? public void setTextColor (int color) 给所有的状态 (normal, selected, focused) 设置成 text 的颜色。 使用 text.setTextColor(#FF0000) 但是不是一个有效的语法。
listview 点击item改变所点击item中的textview的颜色
在listview监听中设置后 item_text = (TextView) parent.getChildAt(position).findViewById(R.id.listtext); item_text.setTextColor(Color.YELLOW); 发现点击后textview变色的文字会随着列表滑动 任意分配位置 求解决
自定义textView的placeHolder被拉伸怎么办
自定义textVIew的placeHolder(灰色文字部分)用quartz2D绘制 我监听键盘的高度 然后在监听方法里面改变textView高度 键盘弹出 textView的高度变小 键盘收回 高度变大 但是,键盘收缩,placeHolder的文字,会被短时间拉伸压缩 然后恢复正常 怎么办? ![图片说明](https://img-ask.csdn.net/upload/201510/18/1445100239_41495.jpg) ``` textView.m -(void)setPlaceHolder:(NSString *)placeHolder{ _placeHolder=placeHolder; self.change=YES; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { //设置textView代理 self.delegate=self; self.backgroundColor=[UIColor whiteColor]; //判断输入内容 NSString *str; if ((self.isChange==YES)&&(self.isClean==NO)) { str=self.placeHolder; //NSLog(@"1"); } if ((self.isChange==NO)&&(self.isClean==NO)) { str=@"请输入内容"; //NSLog(@"2"); } if (self.isClean==YES) { //NSLog(@"3"); str=@""; } //绘制画文字 NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // NSForegroundColorAttributeName : 文字颜色 // NSFontAttributeName : 字体 attrs[NSForegroundColorAttributeName] = [UIColor grayColor]; if (self.fontSize) { attrs[NSFontAttributeName] = [UIFont systemFontOfSize:self.fontSize]; }else{ attrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; } [str drawAtPoint:CGPointMake(5, 8) withAttributes:attrs]; } #pragma mark - 监控textView开始 -(void)textViewDidChange:(UITextView *)textView{ //NSLog(@"4"); self.clean=YES; if (self.text.length==0) { self.clean=NO; } [self setNeedsDisplay]; } - (void)textViewDidBeginEditing:(UITextView *)textView{ //清空placeholder // NSLog(@"5"); self.clean=YES; if (self.text.length==0) { self.clean=NO; } [self setNeedsDisplay]; } #pragma mark - 监控textView结束 -(void)textViewDidEndEditing:(UITextView *)textView{ if (self.text.length==0) { //NSLog(@"6"); self.clean=NO; } [self setNeedsDisplay]; } ``` //controller.m ``` - (void)viewDidLoad { [super viewDidLoad]; self.placeHolderTextView.delegate=self; //隐藏tabbar self.tabBarController.tabBar.hidden=YES; //创建textView self.placeHolderTextView=[[IWTextView alloc]init]; CGFloat textViewX=0; CGFloat textViewY=CGRectGetMaxY(self.timeLabel.frame); CGFloat textViewW=self.view.bounds.size.width; CGFloat textViewH=self.view.bounds.size.height-textViewY; self.placeHolderTextView.frame=CGRectMake(textViewX,textViewY, textViewW, textViewH); //添加进父类 [self.view addSubview:self.placeHolderTextView]; //设置placeHolder self.placeHolderTextView.placeholder=@"请输入备忘事项"; //监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; // MemoModel *model=[[MemoModel alloc]init]; // model.title=self.placeHolderTextView.text; // model.date=self.timeLabel.text; // NSLog(@"123"); } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; //键盘弹出 [self.placeHolderTextView becomeFirstResponder]; } #pragma mark - 键盘frame - (void)KeyboardWillShow:(NSNotification *)notification { //获取键盘frame CGRect keyboardFrame=[notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; //设置textView的frame CGRect textViewFrame=self.placeHolderTextView.frame; textViewFrame.size.height=CGRectGetMinY(keyboardFrame)-CGRectGetMaxY(self.timeLabel.frame); self.placeHolderTextView.frame=textViewFrame; NSLog(@"small%@",NSStringFromCGRect(self.placeHolderTextView.frame)); } - (IBAction)FinishAction:(UIBarButtonItem *)sender { //键盘退出 [self.view endEditing:YES]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } ```
使用textview描边如何动态修改描边的宽度和颜色?
// 文字描边 public class StrokeTextView extends TextView { private TextView outlineTextView = null; public StrokeTextView(Context context) { super(context); outlineTextView = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); outlineTextView = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); outlineTextView = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint paint = outlineTextView.getPaint(); paint.setStrokeWidth(3);// 描边宽度 paint.setStyle(Style.STROKE); outlineTextView.setTextColor(Color.parseColor("#7F00FF"));// 描边颜色 outlineTextView.setGravity(getGravity()); } @Override public void setLayoutParams(ViewGroup.LayoutParams params) { super.setLayoutParams(params); outlineTextView.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 设置轮廓文字 CharSequence outlineText = outlineTextView.getText(); if (outlineText == null || !outlineText.equals(this.getText())) { outlineTextView.setText(getText()); postInvalidate(); } outlineTextView.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); outlineTextView.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { outlineTextView.draw(canvas); super.onDraw(canvas); } } ``` ``` 想在代码中调用这个textview的时候 通过修改参数 动态修改描边宽度和描边颜色这两个值,自己尝试了一下有没有成功,求大神指点一下。。
改变列表视图ListView中的字体和背景
我想改变列表视图ListView中的字体(颜色和大小)和背景。我想用代码改变而不是想通过设置xml的属性实现。以下的list view看起来就像xml: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="18sp" android:text="@string/hello"> 我的代码是: public class NewsActivity extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ArrayAdapter listItemAdapter = new ArrayAdapter( this,android.R.layout.simple_list_item_1, v_itemList ); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,ynetList)); View v=getListView() ; ListView lv = getListView(); 接下来再写什么代码呢?
请问android中的控件被无论如何引用后是不是操作的都是同一个控件
我写了一个简单的test安卓程序,就是点击菜单textview中的文字变色,这个程序是在上一个练习--实现listview显示列表,然后我发现虽然我引用的都是同一个控件,但是只有textview中的文字变色了,可是listview中的文字却没有变色,实在想不明白,莫非不是同一个textview控件吗???? 补上源代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv);//这里我引用了布局文件中的控件 lv = (ListView) findViewById(R.id.lv); //这里我用baseadpter和listview实现了列表输出,在mybaseadapter中我也同样引用了相同的tv MyBaseAdapter mba = new MyBaseAdapter(MainActivity.this, getData()); lv.setAdapter(mba); } public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } //监听菜单选项并定义了监听事件,改变tv的字体颜色 @Override public boolean onOptionsItemSelected(MenuItem item){ super.onOptionsItemSelected(item); switch (item.getItemId()){ case R.id.it1: tv.setTextColor(Color.BLUE); break; case R.id.it2: tv.setTextColor(Color.RED); break; case R.id.it3: tv.setTextColor(Color.BLACK); break; case R.id.it4: break; case R.id.it5: tv.setTextColor(Color.YELLOW); break; case R.id.it6: tv.setTextColor(Color.GREEN); break; } return true; }
Android spinner 可是自定义被点击时的文字效果吗?求助。。
![图片说明](https://img-ask.csdn.net/upload/201508/31/1441001685_414389.png) 怎么改变 spinner 被选择后的文字大小和颜色。在线等。。。 这个效果用什么来做比较好,刚开始选的时候使用 spinner 来写的,但是现在发现要改成这个效果,使用以下方法不管用。 Spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) { // TODO Auto-generated method stub TextView outcomeTextView = (TextView) view.findViewById(android.R.id.text1); String inspecOutcomeString = inspOutcomeSpinner.getSelectedItem().toString(); int outcomeResult = getValueOfInspectionOutcome(inspecOutcomeString); if (outcomeResult == ReviewService.OUTCOME_GOOD ) { outcomeTextView.setTextColor(getResources().getColor(R.color.green)); outcomeTextView.setTextSize(getResources().getDimension(R.dimen.text_large)); }else if (outcomeResult == ReviewService.OUTCOME_IMPROVE) { }else if (outcomeResult == ReviewService.OUTCOME_NOT_DEFINED) { }else if(outcomeResult == ReviewService.OUTCOME_PENDING){ }else if (outcomeResult == ReviewService.OUTCOME_SERIOUS) { } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); }
Android 怎么将主程序添加到欢迎界面里面
我做好了一个欢迎界面,并且做了下按钮触发,里面什么都还没有,我这边有一个游戏主程序,应该怎么样才能合理的添加进去呢? 添加的结果就是 点击这个按钮 就进入这个主程序。下面是代码 这个是我的欢迎界面的代码 package sen.tan; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class dazhuankuaiActivity extends Activity { BallView myview; public static int screenWidth ; public static int screenHeight; private TextView textview; Button kaishi,jieshu,guanyu; ImageView image; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String string = "欢迎进入疯狂打砖块游戏"; image = (ImageView) this.findViewById(R.id.image); image.setImageResource(R.drawable.img1); textview =(TextView) this.findViewById(R.id.textview); textview.setTextSize(20); textview.setTextColor(Color.GREEN); textview.setBackgroundColor(Color.RED); textview.setText(string); kaishi=(Button)findViewById(R.id.button1); guanyu=(Button)findViewById(R.id.button2); jieshu=(Button)findViewById(R.id.button3); kaishi.setWidth(250); guanyu.setWidth(200); jieshu.setWidth(150); kaishi.setTextSize(35); guanyu.setTextSize(29); jieshu.setTextSize(24); jieshu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { dazhuankuaiActivity.this.finish(); } }); guanyu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ textview.setText("本游戏由xxx制作,此游戏控制左右按键,来回移动挡板,不让小球掉落,当小球把上方砖块全部打完时,游戏胜利"); } }); kaishi.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { } }); 下面是我找到的主程序 package ab.cc; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; public class BaomingActivity extends Activity { BallView myView; static int screenWidth; static int screenHeight; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 定义DisplayMetrics对象 */ DisplayMetrics dm = new DisplayMetrics(); /* 取得窗口属性 */ getWindowManager().getDefaultDisplay().getMetrics(dm); /* 窗口的宽度 */ screenWidth = dm.widthPixels; /* 窗口的高度 */ screenHeight = dm.heightPixels; // setTitle("宽"+screenWidth+" 高"+screenHeight); /* 设置为无标题栏 */ requestWindowFeature(Window.FEATURE_NO_TITLE); /* 设置为全屏模式 */ getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); myView = new BallView(this); //设置显示GameSurfaceView视图 setContentView(myView); }//end of onCreate() //触笔事件 public boolean onTouchEvent(MotionEvent event) { return true; } //按键按下事件 public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { //菜单按键-设为初始菜单 case KeyEvent.KEYCODE_MENU: myView.resetGame(); //重新开始 break; //中间按键 case KeyEvent.KEYCODE_DPAD_CENTER: myView.ball_isRun = !myView.ball_isRun;//开始//暂停 break; //左方向键 case KeyEvent.KEYCODE_DPAD_LEFT: if(myView.ball_isRun){ if(myView.board_left<=myView.board_x_move) { myView.board_left=0; myView.board_right=myView.board_length; }else{ myView.board_left-=myView.board_x_move; myView.board_right-=myView.board_x_move;} } break; //右方向键 case KeyEvent.KEYCODE_DPAD_RIGHT: if(myView.ball_isRun){ if(screenWidth-myView.board_right<=myView.board_x_move ) { myView.board_left=screenWidth-myView.board_length; myView.board_right=screenWidth; }else{ myView.board_left+=myView.board_x_move; myView.board_right+=myView.board_x_move; } } break; //上方向键 case KeyEvent.KEYCODE_DPAD_UP: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top) { myView.board_alterable_top-=myView.boardYadd; myView.board_alterable_bottom-=myView.boardYadd; } } break; //下方向键 case KeyEvent.KEYCODE_DPAD_DOWN: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top-myView.boardYadd ) { myView.board_alterable_top=myView.board_default_top; myView.board_alterable_bottom=myView.board_alterable_top+myView.board_thickness; } } break; //返回键 case KeyEvent.KEYCODE_BACK: this.finish(); break; }//end switch return false; } //按键弹起事件 public boolean onKeyUp(int keyCode, KeyEvent event) { return true; } public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return true; } } package ab.cc; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class BallView extends SurfaceView implements SurfaceHolder.Callback, Runnable { //线程延时控制 final int ball_sleep=1;//毫秒,延时越大,球速越慢 final int ball_r= 8;//小球半径 final float ball2_r= 8;//底下滚珠小球半径 final int ballXorYadd = 4;//小球的基本位移。测试可行值:2,4 //获取屏幕宽度和高度 int screen_width;//320; int screen_height;//480; //砖的属性 int brick_width;//每块砖宽 int brick_height;//每块砖高 boolean brick_exist[];//砖是否存在 int k ;// 列//到for循环里才初始化 int j ;// 行 int brick_left = brick_width*(k-1);//到判断语句才初始化 int brick_right = brick_width*k; int brick_top = brick_height*j; int brick_bottom = brick_height*(j+1); //挡板的属性 int board_length;//挡板长度:80比较合适,可以随意修改,但别超过screen_width final int boardYadd = 16;//按上下键时挡板y方向位移量。经验证4、8、16可以,12不行 final int board_x_move = 30;//挡板x方向位移量:可以随意自定义 int board_left;//挡板左侧(可变) int board_right;//挡板右侧(可变) int board_thickness;//挡板厚度 int board_default_top;//即435,挡板的top面初始位置 int board_alterable_top;//挡板上侧(可变) int board_alterable_bottom;//挡板下侧(可变) int ball_default_x;//球的初始x坐标 int ball_default_y;//球的初始y坐标 //球的即时坐标(可变): int ball_x;//球心横坐标 int ball_y;//球心纵坐标 //球的前一步的y坐标 int ball_previous_y; int ball_x_speed;//球的横向偏移量//可变 int ball_y_speed;//球的纵向偏移量//可变 boolean ball_isRun;//球是否在动 // 控制循环 boolean mbLoop; // 定义SurfaceHolder对象 SurfaceHolder mSurfaceHolder = null; //获得分数 int score; /* 唤醒渐变渲染 */ Shader mRadialGradient = null; //------------------------------------------------------------------------------------------------------// public BallView(Context context) { super(context); // 实例化SurfaceHolder mSurfaceHolder = this.getHolder(); // 添加回调 mSurfaceHolder.addCallback(this); this.setFocusable(true); //获取屏幕宽度和高度 screen_width = BaomingActivity.screenWidth;//320 screen_height = BaomingActivity.screenHeight;//480 //砖的属性 brick_width = screen_width/5;//每块砖宽64 brick_height = screen_height/15;//每块砖高32 //挡板的属性 board_length = screen_width/4;//挡板长度:80比较合适,可以随意修改,但别超过screen_width board_left = (screen_width-board_length)/2;//挡板左侧(可变) board_right = (screen_width+board_length)/2;//挡板右侧(可变) board_thickness = 5;//挡板厚度 board_default_top = 13*screen_height/15;//即435,挡板的top面初始位置 board_alterable_top = board_default_top;//挡板上侧(可变) board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧(可变) ball_default_x = screen_width/2;//球的初始x坐标 ball_default_y = board_default_top - ball_r;//球的初始y坐标 //球的即时坐标(可变): ball_x = ball_default_x; ball_y = ball_default_y; //球的前一步的y坐标 ball_previous_y = 0; ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 mbLoop = true; ball_isRun = false; score=0; brick_exist = new boolean[25]; for (int i = 0; i < 25; i++) { brick_exist[i] = true; } /* 构建RadialGradient对象,设置半径的属性 */ mRadialGradient = new RadialGradient(ball_x, ball_y, ball_r,//球中心坐标x,y,半径r new int[]{Color.WHITE,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW},//颜色数组 null,//颜色数组中每一种颜色对应的相对位置,为空的话就是平均分布,由中心向外排布 Shader.TileMode.REPEAT);//渲染模式:重复 } public void resetGame(){ ball_isRun = false; score =0;//分数 ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 ball_x = screen_width/2;//球心起始横坐标 ball_y = board_default_top - ball_r;//球心起始纵坐标 board_left = (screen_width-board_length)/2;//挡板左侧 board_right = (screen_width+board_length)/2;//挡板右侧 board_alterable_top = board_default_top;//挡板上侧 board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧 for (int i = 0; i < 25; i++) { brick_exist[i] = true; } } //---------------------------------绘图循环开始---------------------------------- public void run() { while (mbLoop&&!Thread.currentThread().isInterrupted()) { try { Thread.sleep(ball_sleep); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } //球的前一步y坐标 ball_previous_y = ball_y; if (ball_isRun) { ballRunning();// 让小球移动 boardPositionCheck();//检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” hitWallCheck();//墙壁碰撞检测 hitBoardCheck();//挡板碰撞检测 hitBrickCheck();//砖块碰撞检测 } synchronized (mSurfaceHolder) { Draw(); } } } //---------------------------------绘图循环结束---------------------------------- //------------------------------------------------------------------------------ //让小球移动 public void ballRunning() { ball_x += ball_x_speed; ball_y -= ball_y_speed; } //朝左或朝右碰撞后小球水平方向逆向 public void ballLeftOrRightHit() { ball_x_speed *= -1; } //朝上或朝下碰撞后小球竖直方向逆向 public void ballUpOrDownHit() { ball_y_speed *= -1; } public void ballcornerHit() { ball_x_speed *= -1; ball_y_speed *= -1; } public void ballStraightUp() {//功能没实现,这是多余代码 ball_x_speed = 0;//注意在其他地方恢复 ball_y_speed *= -1; } //-------------------墙壁碰撞检测开始------------------------------------- public void hitWallCheck() { // 左碰墙 if (ball_x <= ball_r && ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); // 右碰墙 } if (ball_x >= screen_width - ball_r && //不能写为 else if,因为可能恰好碰到角落。 ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); } // 上碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y <= ball_r + brick_height) //、、、、、、、、、、、、、、、25号修改 { ballUpOrDownHit(); } // 下碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y >= screen_height-ball_r) { ballUpOrDownHit(); if(score<=10){ score = 0; } else score-=10; } } //-------------------墙壁碰撞检测结束------------------------------------- //-----------------------------挡板碰撞检测开始--------------------------- public void hitBoardCheck() { // 下碰挡板正面 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && //在屏幕内,起码条件 ball_x >= board_left && ball_x <= board_right && //在挡板X域上方 ball_y == board_alterable_top - ball_r &&//球面与挡板相切 ball_previous_y <= board_alterable_top - ball_r //确定球是从上方下落 ) { if(board_alterable_top==board_default_top-boardYadd ){//如果弹簧伸张,挡板位于上线 ballHitBoardlower();//作用:ball_y_move减小;挡板被打下;小球Y向运动反向 } else if(board_alterable_top==board_default_top){//如果弹簧压缩,挡板位于下线 boardHitBallHigher();//作用:ball_y_move增加;挡板弹上;小球Y向运动反向 } } //斜碰挡板右上角//经验证有效 else if(Math.pow(board_right-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>board_right && ball_y<board_alterable_top) { ballcornerHit(); } //斜碰挡板的左上角//经验证有效 else if(Math.pow(board_left-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x<board_left && ball_y<board_alterable_top) { ballcornerHit(); } } //-----------------------------挡板碰撞检测结束-------------------------- private void boardHitBallHigher() {//增强 ballUpOrDownHit();//小球Y方向反向,ball_y_speed变为正数 if(ball_y_speed == ballXorYadd ){ ball_y_speed += ballXorYadd;//离开挡板后小球Y方向速度增强 } if( boardYadd > ball_y_speed ){//在线程这一轮,小球上升多少,挡板就上升多少。 board_alterable_top = board_default_top - ball_y_speed; board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } //检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” private void boardPositionCheck() {//还可直接利用球的位置刷新,board_top与球心相差ball_r if(board_alterable_top < board_default_top && board_alterable_top > board_default_top-boardYadd){ //挡板随球上升 if(board_alterable_top - ball_y_speed >= board_default_top-boardYadd){ board_alterable_top -= ball_y_speed;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } else{ board_alterable_top = board_default_top-boardYadd;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } } private void ballHitBoardlower() {//减弱 board_alterable_top=board_default_top; board_alterable_bottom=board_default_top+board_thickness;//挡板被打退 ballUpOrDownHit();//小球Y方向反向 if(ball_y_speed==2*ballXorYadd){ ball_y_speed -= ballXorYadd;//小球Y方向速度减弱 } } //砖块碰撞检测开始----------------------------------------------------------- public void hitBrickCheck() { for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); //朝下碰砖的top面 AAAAAAAAAAAAAAAAAAAAA if(ball_x >= brick_left && ball_x <= brick_right && ball_y >= brick_top-ball_r && ball_y < brick_top) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝下正碰2砖中间,i砖右上角检测 if(k!=5 && ball_x == brick_right)//如果不是第5列砖的右侧边 { //如果砖[i+1]存在 if(brick_exist[i+1]){ brick_exist[i+1] = false; score+=4; } } //朝下正碰2砖中间,i砖左上角检测 else if(k!=1 && ball_x == brick_left)//如果不是第1列砖的左侧边 { //如果砖[i-1]存在 if(brick_exist[i-1]){ brick_exist[i-1] = false; score+=4; } } } //朝上碰砖的bottom面 BBBBBBBBBBBBBBBBB else if(ball_x >= brick_left && ball_x <= brick_right && ball_y > brick_bottom&& ball_y <= brick_bottom + ball_r ) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝上正碰2块砖中间--i砖的右下角检测 if(k!=5 && ball_x == brick_right) //如果不是第5列砖的右侧边 { if(brick_exist[i+1]){//如果砖[i+1]存在 brick_exist[i+1] = false; score+=4; } } //朝上正碰2块砖中间--i砖的左下角检测 else if(k!=1 && ball_x == brick_left) //如果不是第1列砖的左侧边 { if(brick_exist[i-1]){//如果砖[i-1]存在 brick_exist[i-1] = false; score+=4; } } } //朝右碰砖的left面CCCCCCCCCCCCCCCCC else if(ball_x >= brick_left -ball_r&&ball_x < brick_left&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝右正碰2块砖中间,左下角检测 if(j!=5 && ball_y == brick_bottom)//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝右正碰2块砖中间,左上角检测 else if(j!=1 && ball_y == brick_top)//如果不是第1行砖的上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } //朝左碰砖的right面DDDDDDDDDDDDDDDDDD else if(ball_x >= brick_right && ball_x <= brick_right+ball_r&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝左正碰2块砖中间,右下角检测 if(j!=5 && ball_y == brick_bottom )//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝左正碰2块砖中间,右上角检测 else if(j!=1 && ball_y == brick_top )//如果不是第1行砖上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } ///////////////////////////////////////// //斜碰i砖的左下角EEEEEEEEEEEEEEEEEEEEEEEE else if(( i-1<0||(i-1>=0&&!brick_exist[i-1]) ) && (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_left-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_r && ball_x<brick_left && ball_y>brick_bottom && ball_y<brick_bottom+ball_r ) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右下角FFFFFFFFFFFFFFFFFFFFFFFFFF else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right&&ball_x<brick_right+ball_r&& ball_y>brick_bottom&&ball_y<brick_bottom+ball_r ) { Log.v("----------", "right bottom hit"+i+":"+brick_exist[i]); ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右上角GGGGGGGGGGGGGGGGGGGGGGGG else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i-5<0||(i-5>0&&!brick_exist[i-5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right && ball_x<brick_right+ball_r&& ball_y>brick_top-ball_r && ball_y<brick_top) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的左上角HHHHHHHHHHHHHHHHHHHHHHHHHH else if((i-1<0||(i-1>=0&&!brick_exist[i-1])) && (i-5<0||(i-5>=0&&!brick_exist[i-5])) && Math.pow(brick_left-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_x && ball_x<brick_left && ball_y>brick_top-ball_r && ball_y<brick_top ) { ballcornerHit(); brick_exist[i] = false; score+=4; } }//end if }//end for }//end hitBrickCheck() //砖块碰撞检测结束----------------------------------------------------------- //------------------------------------------ public boolean gameOver(){ int count = 0; for(boolean s:brick_exist){ if(!s){ count++; } } if(count == 25){ return true; }else{ return false; } } //------------------------------------------ //---------------绘图方法开始------------------------ public void Draw() { // 锁定画布,得到canvas Canvas canvas = mSurfaceHolder.lockCanvas(); if (mSurfaceHolder == null || canvas == null) { return; } // 绘图 Paint mPaint = new Paint(); // 设置取消锯齿效果 mPaint.setAntiAlias(true); mPaint.setColor(Color.GREEN); // 绘制矩形--背景 canvas.drawRect(0, 0, screen_width, brick_height-2, mPaint); mPaint.setColor(Color.MAGENTA); // 绘制矩形--背景 canvas.drawRect(0, brick_height-2, screen_width, screen_height, mPaint); mPaint.setColor(Color.RED);//设置字体颜色 mPaint.setTextSize(brick_height-7);//设置字体大小 canvas.drawText("得分:"+score, 0, brick_height-7, mPaint); // 绘制顶层挡板````````````````````````````````````````````````````````25号修改 mPaint.setColor(Color.BLACK);//设置颜色 mPaint.setStrokeWidth(4);//设置粗细 canvas.drawLine(0, brick_height-2, screen_width, brick_height-2, mPaint); for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); mPaint.setStyle(Paint.Style.FILL);// 设置实心画笔 mPaint.setColor(Color.BLACK); canvas.drawRect(brick_left+1, brick_top+1, brick_right-1,brick_bottom-1, mPaint); mPaint.setStyle(Paint.Style.STROKE);// 设置空心画笔 mPaint.setStrokeWidth(2);//设置粗细 mPaint.setColor(Color.RED); canvas.drawRect(brick_left+3, brick_top+3, brick_right-3,brick_bottom-3, mPaint); } } // 设置实心画笔 mPaint.setStyle(Paint.Style.FILL); { mPaint.setShader(mRadialGradient); canvas.drawCircle(ball_x, ball_y, ball_r, mPaint); } Paint mPaint2 = new Paint(); // 设置取消锯齿效果 mPaint2.setAntiAlias(true); // 设置实心画笔 mPaint2.setStyle(Paint.Style.FILL); { mPaint2.setColor(Color.BLACK); /* 绘制矩形挡板 */ canvas.drawRect(board_left, board_alterable_top, board_right, board_alterable_bottom,mPaint2); float board2_bottom = screen_height - 2*ball2_r;//164 float board2_top = board2_bottom - 4;//160 //线段端点坐标数组 float x0 = board_left+(board_right-board_left)/4; float y0 = board_alterable_bottom;//440或者444 float springAdd = (board2_top- board_alterable_bottom)/8;//即2.5或者2:弹簧小线段的y向长度 float springWidth = 5.0f;//弹簧小线段的x向长度 float x1 = x0 - springWidth; float y1 = y0 + springAdd; float x2 = x0 + springWidth; float y2 = y0 + 3*springAdd; float x3 = x1 ; float y3 = y0 + 5*springAdd; float x4 = x2; float y4 = y0 + 7*springAdd; float x5 = x0 ; float y5 = board2_top;//即460 float between_spring = (board_right-board_left)/2; float pts[] = { x0,y0,x1,y1, x1,y1,x2,y2, x2,y2,x3,y3, x3,y3,x4,y4, x4,y4,x5,y5}; float pts2[] = {x0+between_spring,y0, x1+between_spring,y1, x1+between_spring,y1, x2+between_spring,y2, x2+between_spring,y2, x3+between_spring,y3, x3+between_spring,y3, x4+between_spring,y4, x4+between_spring,y4, x5+between_spring,y5}; mPaint2.setStrokeWidth(2);//设置弹簧粗细 //绘制2个弹簧 canvas.drawLines(pts, mPaint2); canvas.drawLines(pts2, mPaint2); //绘制下层挡板 canvas.drawRect(board_left, board2_top, board_right, board2_bottom,mPaint2); mPaint2.setColor(Color.BLACK);// // 绘制最下面的两个“轮子”(圆心x,圆心y,半径r,p) canvas.drawCircle(board_left+ball2_r, screen_height-ball2_r, ball2_r, mPaint2);//圆 canvas.drawCircle(board_right-ball2_r, screen_height-ball2_r, ball2_r, mPaint2); mPaint2.setColor(Color.WHITE);// }//实心画笔mPaint2结束 if(gameOver()){ // mbLoop = false;//注释掉的话GAME OVER后也可重启 ball_isRun = false; mPaint2.setColor(Color.BLACK);//设置字体颜色 mPaint2.setTextSize(40.0f);//设置字体大小 canvas.drawText("GAME OVER", screen_width/32+40, screen_height/16*9-70, mPaint2); mPaint2.setTextSize(20.0f);//设置字体大小 canvas.drawText("Press \"MENU\" button to restart,", screen_width/32, //320/32=10 screen_height/16*9, //480/16=30,30*9=270 mPaint2); canvas.drawText("Press \"BACK\" button to exit.", screen_width/32, //320/32=10 screen_height/16*10, //480/16=30,30*10=300 mPaint2); } // 绘制后解锁,绘制后必须解锁才能显示 mSurfaceHolder.unlockCanvasAndPost(canvas); }// end of Draw() //---------------------绘图方法结束----------------------------------------------- // 在surface的大小发生改变时激发 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } // 在surface创建时激发 public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start();// 开启绘图线程 } // 在surface销毁时激发 public void surfaceDestroyed(SurfaceHolder holder) { // 停止循环 mbLoop = false; } } 能否回答的详细一点,学的时间短,求帮忙
所做Android打砖块代码其中 出错 求教
下面是代码 可其中出现了一个错误(做了标记),不知道怎么解决,可否有高手帮我看看,能不能稍作修改一下,做成正确的。 就是这句错了 myview = new BallView(this); package sen.tan; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class dazhuankuaiActivity extends Activity { BallView myview; public static int screenWidth ; public static int screenHeight; private TextView textview; Button kaishi,jieshu,guanyu; ImageView image; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String string = "欢迎进入疯狂打砖块游戏"; image = (ImageView) this.findViewById(R.id.image); image.setImageResource(R.drawable.img1); textview =(TextView) this.findViewById(R.id.textview); textview.setTextSize(20); textview.setTextColor(Color.GREEN); textview.setBackgroundColor(Color.RED); textview.setText(string); kaishi=(Button)findViewById(R.id.button1); guanyu=(Button)findViewById(R.id.button2); jieshu=(Button)findViewById(R.id.button3); kaishi.setWidth(250); guanyu.setWidth(200); jieshu.setWidth(150); kaishi.setTextSize(35); guanyu.setTextSize(29); jieshu.setTextSize(24); jieshu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { dazhuankuaiActivity.this.finish(); } }); guanyu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ textview.setText("本游戏由xxx制作,此游戏控制左右按键,来回移动挡板,不让小球掉落,当小球把上方砖块全部打完时,游戏胜利"); } }); kaishi.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; setTitle("宽"+screenWidth+" 高"+screenHeight); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); `myview = new BallView(this);` //错误在这里 setContentView(myview); } public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_MENU: myview.resetGame(); break; case KeyEvent.KEYCODE_DPAD_CENTER: myview.ball_isRun = !myview.ball_isRun; break; case KeyEvent.KEYCODE_DPAD_LEFT: if(myview.ball_isRun){ if(myview.board_left<=myview.board_x_move) { myview.board_left=0; myview.board_right=myview.board_length; }else{ myview.board_left-=myview.board_x_move; myview.board_right-=myview.board_x_move;} } break; case KeyEvent.KEYCODE_DPAD_RIGHT: if(myview.ball_isRun){ if(screenWidth-myview.board_right<=myview.board_x_move ) { myview.board_left=screenWidth-myview.board_length; myview.board_right=screenWidth; }else{ myview.board_left+=myview.board_x_move; myview.board_right+=myview.board_x_move; } } break; case KeyEvent.KEYCODE_DPAD_UP: if(myview.ball_isRun){ if( myview.board_alterable_top==myview.board_default_top) { myview.board_alterable_top-=myview.boardYadd; myview.board_alterable_bottom-=myview.boardYadd; } } break; case KeyEvent.KEYCODE_DPAD_DOWN: if(myview.ball_isRun){ if( myview.board_alterable_top==myview.board_default_top-myview.boardYadd ) { myview.board_alterable_top=myview.board_default_top; myview.board_alterable_bottom=myview.board_alterable_top+myview.board_thickness; } } break; case KeyEvent.KEYCODE_BACK: dazhuankuaiActivity.this.finish(); break; }//end switch return false; } //按键弹起事件 } ); } } package sen.tan; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class BallView extends SurfaceView implements SurfaceHolder.Callback, Runnable { //线程延时控制 final int ball_sleep=1;//毫秒,延时越大,球速越慢 final int ball_r= 8;//小球半径 final float ball2_r= 8;//底下滚珠小球半径 final int ballXorYadd = 4;//小球的基本位移。测试可行值:2,4 //获取屏幕宽度和高度 int screen_width;//320; int screen_height;//480; //砖的属性 int brick_width;//每块砖宽 int brick_height;//每块砖高 boolean brick_exist[];//砖是否存在 int k ;// 列//到for循环里才初始化 int j ;// 行 int brick_left = brick_width*(k-1);//到判断语句才初始化 int brick_right = brick_width*k; int brick_top = brick_height*j; int brick_bottom = brick_height*(j+1); //挡板的属性 int board_length;//挡板长度:80比较合适,可以随意修改,但别超过screen_width final int boardYadd = 16;//按上下键时挡板y方向位移量。经验证4、8、16可以,12不行 final int board_x_move = 30;//挡板x方向位移量:可以随意自定义 int board_left;//挡板左侧(可变) int board_right;//挡板右侧(可变) int board_thickness;//挡板厚度 int board_default_top;//即435,挡板的top面初始位置 int board_alterable_top;//挡板上侧(可变) int board_alterable_bottom;//挡板下侧(可变) int ball_default_x;//球的初始x坐标 int ball_default_y;//球的初始y坐标 //球的即时坐标(可变): int ball_x;//球心横坐标 int ball_y;//球心纵坐标 //球的前一步的y坐标 int ball_previous_y; int ball_x_speed;//球的横向偏移量//可变 int ball_y_speed;//球的纵向偏移量//可变 boolean ball_isRun;//球是否在动 // 控制循环 boolean mbLoop; // 定义SurfaceHolder对象 SurfaceHolder mSurfaceHolder = null; //获得分数 int score; /* 唤醒渐变渲染 */ Shader mRadialGradient = null; //------------------------------------------------------------------------------------------------------// public BallView(Context context) { super(context); // 实例化SurfaceHolder mSurfaceHolder = this.getHolder(); // 添加回调 mSurfaceHolder.addCallback(this); this.setFocusable(true); //获取屏幕宽度和高度 screen_width = dazhuankuaiActivity.screenWidth;//320 screen_height = dazhuankuaiActivity.screenHeight;//480 //砖的属性 brick_width = screen_width/5;//每块砖宽64 brick_height = screen_height/15;//每块砖高32 //挡板的属性 board_length = screen_width/4;//挡板长度:80比较合适,可以随意修改,但别超过screen_width board_left = (screen_width-board_length)/2;//挡板左侧(可变) board_right = (screen_width+board_length)/2;//挡板右侧(可变) board_thickness = 5;//挡板厚度 board_default_top = 13*screen_height/15;//即435,挡板的top面初始位置 board_alterable_top = board_default_top;//挡板上侧(可变) board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧(可变) ball_default_x = screen_width/2;//球的初始x坐标 ball_default_y = board_default_top - ball_r;//球的初始y坐标 //球的即时坐标(可变): ball_x = ball_default_x; ball_y = ball_default_y; //球的前一步的y坐标 ball_previous_y = 0; ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 mbLoop = true; ball_isRun = false; score=0; brick_exist = new boolean[25]; for (int i = 0; i < 25; i++) { brick_exist[i] = true; } /* 构建RadialGradient对象,设置半径的属性 */ mRadialGradient = new RadialGradient(ball_x, ball_y, ball_r,//球中心坐标x,y,半径r new int[]{Color.WHITE,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW},//颜色数组 null,//颜色数组中每一种颜色对应的相对位置,为空的话就是平均分布,由中心向外排布 Shader.TileMode.REPEAT);//渲染模式:重复 } public void resetGame(){ ball_isRun = false; score =0;//分数 ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 ball_x = screen_width/2;//球心起始横坐标 ball_y = board_default_top - ball_r;//球心起始纵坐标 board_left = (screen_width-board_length)/2;//挡板左侧 board_right = (screen_width+board_length)/2;//挡板右侧 board_alterable_top = board_default_top;//挡板上侧 board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧 for (int i = 0; i < 25; i++) { brick_exist[i] = true; } } //---------------------------------绘图循环开始---------------------------------- public void run() { while (mbLoop&&!Thread.currentThread().isInterrupted()) { try { Thread.sleep(ball_sleep); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } //球的前一步y坐标 ball_previous_y = ball_y; if (ball_isRun) { ballRunning();// 让小球移动 boardPositionCheck();//检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” hitWallCheck();//墙壁碰撞检测 hitBoardCheck();//挡板碰撞检测 hitBrickCheck();//砖块碰撞检测 } synchronized (mSurfaceHolder) { Draw(); } } } //---------------------------------绘图循环结束---------------------------------- //------------------------------------------------------------------------------ //让小球移动 public void ballRunning() { ball_x += ball_x_speed; ball_y -= ball_y_speed; } //朝左或朝右碰撞后小球水平方向逆向 public void ballLeftOrRightHit() { ball_x_speed *= -1; } //朝上或朝下碰撞后小球竖直方向逆向 public void ballUpOrDownHit() { ball_y_speed *= -1; } public void ballcornerHit() { ball_x_speed *= -1; ball_y_speed *= -1; } public void ballStraightUp() {//功能没实现,这是多余代码 ball_x_speed = 0;//注意在其他地方恢复 ball_y_speed *= -1; } //-------------------墙壁碰撞检测开始------------------------------------- public void hitWallCheck() { // 左碰墙 if (ball_x <= ball_r && ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); // 右碰墙 } if (ball_x >= screen_width - ball_r && //不能写为 else if,因为可能恰好碰到角落。 ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); } // 上碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y <= ball_r + brick_height) //、、、、、、、、、、、、、、、25号修改 { ballUpOrDownHit(); } // 下碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y >= screen_height-ball_r) { ballUpOrDownHit(); if(score<=10){ score = 0; } else score-=10; } } //-------------------墙壁碰撞检测结束------------------------------------- //-----------------------------挡板碰撞检测开始--------------------------- public void hitBoardCheck() { // 下碰挡板正面 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && //在屏幕内,起码条件 ball_x >= board_left && ball_x <= board_right && //在挡板X域上方 ball_y == board_alterable_top - ball_r &&//球面与挡板相切 ball_previous_y <= board_alterable_top - ball_r //确定球是从上方下落 ) { if(board_alterable_top==board_default_top-boardYadd ){//如果弹簧伸张,挡板位于上线 ballHitBoardlower();//作用:ball_y_move减小;挡板被打下;小球Y向运动反向 } else if(board_alterable_top==board_default_top){//如果弹簧压缩,挡板位于下线 boardHitBallHigher();//作用:ball_y_move增加;挡板弹上;小球Y向运动反向 } } //斜碰挡板右上角//经验证有效 else if(Math.pow(board_right-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>board_right && ball_y<board_alterable_top) { ballcornerHit(); } //斜碰挡板的左上角//经验证有效 else if(Math.pow(board_left-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x<board_left && ball_y<board_alterable_top) { ballcornerHit(); } } //-----------------------------挡板碰撞检测结束-------------------------- private void boardHitBallHigher() {//增强 ballUpOrDownHit();//小球Y方向反向,ball_y_speed变为正数 if(ball_y_speed == ballXorYadd ){ ball_y_speed += ballXorYadd;//离开挡板后小球Y方向速度增强 } if( boardYadd > ball_y_speed ){//在线程这一轮,小球上升多少,挡板就上升多少。 board_alterable_top = board_default_top - ball_y_speed; board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } //检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” private void boardPositionCheck() {//还可直接利用球的位置刷新,board_top与球心相差ball_r if(board_alterable_top < board_default_top && board_alterable_top > board_default_top-boardYadd){ //挡板随球上升 if(board_alterable_top - ball_y_speed >= board_default_top-boardYadd){ board_alterable_top -= ball_y_speed;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } else{ board_alterable_top = board_default_top-boardYadd;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } } private void ballHitBoardlower() {//减弱 board_alterable_top=board_default_top; board_alterable_bottom=board_default_top+board_thickness;//挡板被打退 ballUpOrDownHit();//小球Y方向反向 if(ball_y_speed==2*ballXorYadd){ ball_y_speed -= ballXorYadd;//小球Y方向速度减弱 } } //砖块碰撞检测开始----------------------------------------------------------- public void hitBrickCheck() { for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); //朝下碰砖的top面 AAAAAAAAAAAAAAAAAAAAA if(ball_x >= brick_left && ball_x <= brick_right && ball_y >= brick_top-ball_r && ball_y < brick_top) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝下正碰2砖中间,i砖右上角检测 if(k!=5 && ball_x == brick_right)//如果不是第5列砖的右侧边 { //如果砖[i+1]存在 if(brick_exist[i+1]){ brick_exist[i+1] = false; score+=4; } } //朝下正碰2砖中间,i砖左上角检测 else if(k!=1 && ball_x == brick_left)//如果不是第1列砖的左侧边 { //如果砖[i-1]存在 if(brick_exist[i-1]){ brick_exist[i-1] = false; score+=4; } } } //朝上碰砖的bottom面 BBBBBBBBBBBBBBBBB else if(ball_x >= brick_left && ball_x <= brick_right && ball_y > brick_bottom&& ball_y <= brick_bottom + ball_r ) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝上正碰2块砖中间--i砖的右下角检测 if(k!=5 && ball_x == brick_right) //如果不是第5列砖的右侧边 { if(brick_exist[i+1]){//如果砖[i+1]存在 brick_exist[i+1] = false; score+=4; } } //朝上正碰2块砖中间--i砖的左下角检测 else if(k!=1 && ball_x == brick_left) //如果不是第1列砖的左侧边 { if(brick_exist[i-1]){//如果砖[i-1]存在 brick_exist[i-1] = false; score+=4; } } } //朝右碰砖的left面CCCCCCCCCCCCCCCCC else if(ball_x >= brick_left -ball_r&&ball_x < brick_left&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝右正碰2块砖中间,左下角检测 if(j!=5 && ball_y == brick_bottom)//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝右正碰2块砖中间,左上角检测 else if(j!=1 && ball_y == brick_top)//如果不是第1行砖的上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } //朝左碰砖的right面DDDDDDDDDDDDDDDDDD else if(ball_x >= brick_right && ball_x <= brick_right+ball_r&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝左正碰2块砖中间,右下角检测 if(j!=5 && ball_y == brick_bottom )//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝左正碰2块砖中间,右上角检测 else if(j!=1 && ball_y == brick_top )//如果不是第1行砖上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } ///////////////////////////////////////// //斜碰i砖的左下角EEEEEEEEEEEEEEEEEEEEEEEE else if(( i-1<0||(i-1>=0&&!brick_exist[i-1]) ) && (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_left-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_r && ball_x<brick_left && ball_y>brick_bottom && ball_y<brick_bottom+ball_r ) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右下角FFFFFFFFFFFFFFFFFFFFFFFFFF else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right&&ball_x<brick_right+ball_r&& ball_y>brick_bottom&&ball_y<brick_bottom+ball_r ) { Log.v("----------", "right bottom hit"+i+":"+brick_exist[i]); ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右上角GGGGGGGGGGGGGGGGGGGGGGGG else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i-5<0||(i-5>0&&!brick_exist[i-5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right && ball_x<brick_right+ball_r&& ball_y>brick_top-ball_r && ball_y<brick_top) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的左上角HHHHHHHHHHHHHHHHHHHHHHHHHH else if((i-1<0||(i-1>=0&&!brick_exist[i-1])) && (i-5<0||(i-5>=0&&!brick_exist[i-5])) && Math.pow(brick_left-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_x && ball_x<brick_left && ball_y>brick_top-ball_r && ball_y<brick_top ) { ballcornerHit(); brick_exist[i] = false; score+=4; } }//end if }//end for }//end hitBrickCheck() //砖块碰撞检测结束----------------------------------------------------------- //------------------------------------------ public boolean gameOver(){ int count = 0; for(boolean s:brick_exist){ if(!s){ count++; } } if(count == 25){ return true; }else{ return false; } } //------------------------------------------ //---------------绘图方法开始------------------------ public void Draw() { // 锁定画布,得到canvas Canvas canvas = mSurfaceHolder.lockCanvas(); if (mSurfaceHolder == null || canvas == null) { return; } // 绘图 Paint mPaint = new Paint(); // 设置取消锯齿效果 mPaint.setAntiAlias(true); mPaint.setColor(Color.BLACK); // 绘制矩形--背景 canvas.drawRect(0, 0, screen_width, brick_height-2, mPaint); mPaint.setColor(Color.GREEN); // 绘制矩形--背景 canvas.drawRect(0, brick_height-2, screen_width, screen_height, mPaint); mPaint.setColor(Color.RED);//设置字体颜色 mPaint.setTextSize(brick_height-7);//设置字体大小 canvas.drawText("得分:"+score, 0, brick_height-7, mPaint); // 绘制顶层挡板````````````````````````````````````````````````````````25号修改 mPaint.setColor(Color.BLACK);//设置颜色 mPaint.setStrokeWidth(4);//设置粗细 canvas.drawLine(0, brick_height-2, screen_width, brick_height-2, mPaint); for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); mPaint.setStyle(Paint.Style.FILL);// 设置实心画笔 mPaint.setColor(Color.YELLOW); canvas.drawRect(brick_left+1, brick_top+1, brick_right-1,brick_bottom-1, mPaint); mPaint.setStyle(Paint.Style.STROKE);// 设置空心画笔 mPaint.setStrokeWidth(2);//设置粗细 mPaint.setColor(Color.BLUE); canvas.drawRect(brick_left+3, brick_top+3, brick_right-3,brick_bottom-3, mPaint); } } // 设置实心画笔 mPaint.setStyle(Paint.Style.FILL); { mPaint.setShader(mRadialGradient); canvas.drawCircle(ball_x, ball_y, ball_r, mPaint); } Paint mPaint2 = new Paint(); // 设置取消锯齿效果 mPaint2.setAntiAlias(true); // 设置实心画笔 mPaint2.setStyle(Paint.Style.FILL); { mPaint2.setColor(Color.BLACK); /* 绘制矩形挡板 */ canvas.drawRect(board_left, board_alterable_top, board_right, board_alterable_bottom,mPaint2); float board2_bottom = screen_height - 2*ball2_r;//164 float board2_top = board2_bottom - 4;//160 //线段端点坐标数组 float x0 = board_left+(board_right-board_left)/4; float y0 = board_alterable_bottom;//440或者444 float springAdd = (board2_top- board_alterable_bottom)/8;//即2.5或者2:弹簧小线段的y向长度 float springWidth = 5.0f;//弹簧小线段的x向长度 float x1 = x0 - springWidth; float y1 = y0 + springAdd; float x2 = x0 + springWidth; float y2 = y0 + 3*springAdd; float x3 = x1 ; float y3 = y0 + 5*springAdd; float x4 = x2; float y4 = y0 + 7*springAdd; float x5 = x0 ; float y5 = board2_top;//即460 float between_spring = (board_right-board_left)/2; float pts[] = { x0,y0,x1,y1, x1,y1,x2,y2, x2,y2,x3,y3, x3,y3,x4,y4, x4,y4,x5,y5}; float pts2[] = {x0+between_spring,y0, x1+between_spring,y1, x1+between_spring,y1, x2+between_spring,y2, x2+between_spring,y2, x3+between_spring,y3, x3+between_spring,y3, x4+between_spring,y4, x4+between_spring,y4, x5+between_spring,y5}; mPaint2.setStrokeWidth(2);//设置弹簧粗细 //绘制2个弹簧 canvas.drawLines(pts, mPaint2); canvas.drawLines(pts2, mPaint2); //绘制下层挡板 canvas.drawRect(board_left, board2_top, board_right, board2_bottom,mPaint2); mPaint2.setColor(Color.BLACK);// // 绘制最下面的两个“轮子”(圆心x,圆心y,半径r,p) canvas.drawCircle(board_left+ball2_r, screen_height-ball2_r, ball2_r, mPaint2);//圆 canvas.drawCircle(board_right-ball2_r, screen_height-ball2_r, ball2_r, mPaint2); mPaint2.setColor(Color.WHITE);// canvas.drawPoint(board_left+ball2_r, screen_height-ball2_r, mPaint2);//绘制左轮轮心 canvas.drawPoint(board_right-ball2_r, screen_height-ball2_r, mPaint2);//绘制右轮轮心 }//实心画笔mPaint2结束 if(gameOver()){ // mbLoop = false;//注释掉的话GAME OVER后也可重启 ball_isRun = false; mPaint2.setColor(Color.BLACK);//设置字体颜色 mPaint2.setTextSize(40.0f);//设置字体大小 canvas.drawText("GAME OVER", screen_width/32+40, screen_height/16*9-70, mPaint2); mPaint2.setTextSize(20.0f);//设置字体大小 canvas.drawText("Press \"MENU\" button to restart,", screen_width/32, //320/32=10 screen_height/16*9, //480/16=30,30*9=270 mPaint2); canvas.drawText("Press \"BACK\" button to exit.", screen_width/32, //320/32=10 screen_height/16*10, //480/16=30,30*10=300 mPaint2); } // 绘制后解锁,绘制后必须解锁才能显示 mSurfaceHolder.unlockCanvasAndPost(canvas); }// end of Draw() //---------------------绘图方法结束----------------------------------------------- // 在surface的大小发生改变时激发 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } // 在surface创建时激发 public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start();// 开启绘图线程 } // 在surface销毁时激发 public void surfaceDestroyed(SurfaceHolder holder) { // 停止循环 mbLoop = false; } } 下个界面的代码是 package ab.cc; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; public class BaomingActivity extends Activity { BallView myView; static int screenWidth; static int screenHeight; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 定义DisplayMetrics对象 */ DisplayMetrics dm = new DisplayMetrics(); /* 取得窗口属性 */ getWindowManager().getDefaultDisplay().getMetrics(dm); /* 窗口的宽度 */ screenWidth = dm.widthPixels; /* 窗口的高度 */ screenHeight = dm.heightPixels; // setTitle("宽"+screenWidth+" 高"+screenHeight); /* 设置为无标题栏 */ requestWindowFeature(Window.FEATURE_NO_TITLE); /* 设置为全屏模式 */ getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); myView = new BallView(this); //设置显示GameSurfaceView视图 setContentView(myView); }//end of onCreate() //触笔事件 public boolean onTouchEvent(MotionEvent event) { return true; } //按键按下事件 public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { //菜单按键-设为初始菜单 case KeyEvent.KEYCODE_MENU: myView.resetGame(); //重新开始 break; //中间按键 case KeyEvent.KEYCODE_DPAD_CENTER: myView.ball_isRun = !myView.ball_isRun;//开始//暂停 break; //左方向键 case KeyEvent.KEYCODE_DPAD_LEFT: if(myView.ball_isRun){ if(myView.board_left<=myView.board_x_move) { myView.board_left=0; myView.board_right=myView.board_length; }else{ myView.board_left-=myView.board_x_move; myView.board_right-=myView.board_x_move;} } break; //右方向键 case KeyEvent.KEYCODE_DPAD_RIGHT: if(myView.ball_isRun){ if(screenWidth-myView.board_right<=myView.board_x_move ) { myView.board_left=screenWidth-myView.board_length; myView.board_right=screenWidth; }else{ myView.board_left+=myView.board_x_move; myView.board_right+=myView.board_x_move; } } break; //上方向键 case KeyEvent.KEYCODE_DPAD_UP: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top) { myView.board_alterable_top-=myView.boardYadd; myView.board_alterable_bottom-=myView.boardYadd; } } break; //下方向键 case KeyEvent.KEYCODE_DPAD_DOWN: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top-myView.boardYadd ) { myView.board_alterable_top=myView.board_default_top; myView.board_alterable_bottom=myView.board_alterable_top+myView.board_thickness; } } break; //返回键 case KeyEvent.KEYCODE_BACK: this.finish(); break; }//end switch return false; } //按键弹起事件 public boolean onKeyUp(int keyCode, KeyEvent event) { return true; } public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return true; } }
Android打砖块代码 其中系统说格式错误 求帮助
我这边有一个打砖块代码,我将它移植到我的按钮事件触发里 提示我格式错误 下面前两个是打砖块代码 ,后两个是转到我的里面之后的, 标注了,请帮忙。 package ab.cc; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.Window; import android.view.WindowManager; public class BaomingActivity extends Activity { BallView myView; static int screenWidth; static int screenHeight; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 定义DisplayMetrics对象 */ DisplayMetrics dm = new DisplayMetrics(); /* 取得窗口属性 */ getWindowManager().getDefaultDisplay().getMetrics(dm); /* 窗口的宽度 */ screenWidth = dm.widthPixels; /* 窗口的高度 */ screenHeight = dm.heightPixels; // setTitle("宽"+screenWidth+" 高"+screenHeight); /* 设置为无标题栏 */ requestWindowFeature(Window.FEATURE_NO_TITLE); /* 设置为全屏模式 */ getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); myView = new BallView(this); //设置显示GameSurfaceView视图 setContentView(myView); }//end of onCreate() //触笔事件 public boolean onTouchEvent(MotionEvent event) { return true; } //按键按下事件 public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { //菜单按键-设为初始菜单 case KeyEvent.KEYCODE_MENU: myView.resetGame(); //重新开始 break; //中间按键 case KeyEvent.KEYCODE_DPAD_CENTER: myView.ball_isRun = !myView.ball_isRun;//开始//暂停 break; //左方向键 case KeyEvent.KEYCODE_DPAD_LEFT: if(myView.ball_isRun){ if(myView.board_left<=myView.board_x_move) { myView.board_left=0; myView.board_right=myView.board_length; }else{ myView.board_left-=myView.board_x_move; myView.board_right-=myView.board_x_move;} } break; //右方向键 case KeyEvent.KEYCODE_DPAD_RIGHT: if(myView.ball_isRun){ if(screenWidth-myView.board_right<=myView.board_x_move ) { myView.board_left=screenWidth-myView.board_length; myView.board_right=screenWidth; }else{ myView.board_left+=myView.board_x_move; myView.board_right+=myView.board_x_move; } } break; //上方向键 case KeyEvent.KEYCODE_DPAD_UP: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top) { myView.board_alterable_top-=myView.boardYadd; myView.board_alterable_bottom-=myView.boardYadd; } } break; //下方向键 case KeyEvent.KEYCODE_DPAD_DOWN: if(myView.ball_isRun){ if( myView.board_alterable_top==myView.board_default_top-myView.boardYadd ) { myView.board_alterable_top=myView.board_default_top; myView.board_alterable_bottom=myView.board_alterable_top+myView.board_thickness; } } break; //返回键 case KeyEvent.KEYCODE_BACK: this.finish(); break; }//end switch return false; } //按键弹起事件 public boolean onKeyUp(int keyCode, KeyEvent event) { return true; } public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return true; } } package ab.cc; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RadialGradient; import android.graphics.Shader; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class BallView extends SurfaceView implements SurfaceHolder.Callback, Runnable { //线程延时控制 final int ball_sleep=1;//毫秒,延时越大,球速越慢 final int ball_r= 8;//小球半径 final float ball2_r= 8;//底下滚珠小球半径 final int ballXorYadd = 4;//小球的基本位移。测试可行值:2,4 //获取屏幕宽度和高度 int screen_width;//320; int screen_height;//480; //砖的属性 int brick_width;//每块砖宽 int brick_height;//每块砖高 boolean brick_exist[];//砖是否存在 int k ;// 列//到for循环里才初始化 int j ;// 行 int brick_left = brick_width*(k-1);//到判断语句才初始化 int brick_right = brick_width*k; int brick_top = brick_height*j; int brick_bottom = brick_height*(j+1); //挡板的属性 int board_length;//挡板长度:80比较合适,可以随意修改,但别超过screen_width final int boardYadd = 16;//按上下键时挡板y方向位移量。经验证4、8、16可以,12不行 final int board_x_move = 30;//挡板x方向位移量:可以随意自定义 int board_left;//挡板左侧(可变) int board_right;//挡板右侧(可变) int board_thickness;//挡板厚度 int board_default_top;//即435,挡板的top面初始位置 int board_alterable_top;//挡板上侧(可变) int board_alterable_bottom;//挡板下侧(可变) int ball_default_x;//球的初始x坐标 int ball_default_y;//球的初始y坐标 //球的即时坐标(可变): int ball_x;//球心横坐标 int ball_y;//球心纵坐标 //球的前一步的y坐标 int ball_previous_y; int ball_x_speed;//球的横向偏移量//可变 int ball_y_speed;//球的纵向偏移量//可变 boolean ball_isRun;//球是否在动 // 控制循环 boolean mbLoop; // 定义SurfaceHolder对象 SurfaceHolder mSurfaceHolder = null; //获得分数 int score; /* 唤醒渐变渲染 */ Shader mRadialGradient = null; //------------------------------------------------------------------------------------------------------// public BallView(Context context) { super(context); // 实例化SurfaceHolder mSurfaceHolder = this.getHolder(); // 添加回调 mSurfaceHolder.addCallback(this); this.setFocusable(true); //获取屏幕宽度和高度 screen_width = BaomingActivity.screenWidth;//320 screen_height = BaomingActivity.screenHeight;//480 //砖的属性 brick_width = screen_width/5;//每块砖宽64 brick_height = screen_height/15;//每块砖高32 //挡板的属性 board_length = screen_width/4;//挡板长度:80比较合适,可以随意修改,但别超过screen_width board_left = (screen_width-board_length)/2;//挡板左侧(可变) board_right = (screen_width+board_length)/2;//挡板右侧(可变) board_thickness = 5;//挡板厚度 board_default_top = 13*screen_height/15;//即435,挡板的top面初始位置 board_alterable_top = board_default_top;//挡板上侧(可变) board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧(可变) ball_default_x = screen_width/2;//球的初始x坐标 ball_default_y = board_default_top - ball_r;//球的初始y坐标 //球的即时坐标(可变): ball_x = ball_default_x; ball_y = ball_default_y; //球的前一步的y坐标 ball_previous_y = 0; ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 mbLoop = true; ball_isRun = false; score=0; brick_exist = new boolean[25]; for (int i = 0; i < 25; i++) { brick_exist[i] = true; } /* 构建RadialGradient对象,设置半径的属性 */ mRadialGradient = new RadialGradient(ball_x, ball_y, ball_r,//球中心坐标x,y,半径r new int[]{Color.WHITE,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW},//颜色数组 null,//颜色数组中每一种颜色对应的相对位置,为空的话就是平均分布,由中心向外排布 Shader.TileMode.REPEAT);//渲染模式:重复 } public void resetGame(){ ball_isRun = false; score =0;//分数 ball_x_speed = ballXorYadd;//球的横向偏移量 ball_y_speed = ballXorYadd;//球的纵向偏移量 ball_x = screen_width/2;//球心起始横坐标 ball_y = board_default_top - ball_r;//球心起始纵坐标 board_left = (screen_width-board_length)/2;//挡板左侧 board_right = (screen_width+board_length)/2;//挡板右侧 board_alterable_top = board_default_top;//挡板上侧 board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧 for (int i = 0; i < 25; i++) { brick_exist[i] = true; } } //---------------------------------绘图循环开始---------------------------------- public void run() { while (mbLoop&&!Thread.currentThread().isInterrupted()) { try { Thread.sleep(ball_sleep); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } //球的前一步y坐标 ball_previous_y = ball_y; if (ball_isRun) { ballRunning();// 让小球移动 boardPositionCheck();//检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” hitWallCheck();//墙壁碰撞检测 hitBoardCheck();//挡板碰撞检测 hitBrickCheck();//砖块碰撞检测 } synchronized (mSurfaceHolder) { Draw(); } } } //---------------------------------绘图循环结束---------------------------------- //------------------------------------------------------------------------------ //让小球移动 public void ballRunning() { ball_x += ball_x_speed; ball_y -= ball_y_speed; } //朝左或朝右碰撞后小球水平方向逆向 public void ballLeftOrRightHit() { ball_x_speed *= -1; } //朝上或朝下碰撞后小球竖直方向逆向 public void ballUpOrDownHit() { ball_y_speed *= -1; } public void ballcornerHit() { ball_x_speed *= -1; ball_y_speed *= -1; } public void ballStraightUp() {//功能没实现,这是多余代码 ball_x_speed = 0;//注意在其他地方恢复 ball_y_speed *= -1; } //-------------------墙壁碰撞检测开始------------------------------------- public void hitWallCheck() { // 左碰墙 if (ball_x <= ball_r && ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); // 右碰墙 } if (ball_x >= screen_width - ball_r && //不能写为 else if,因为可能恰好碰到角落。 ball_y >= ball_r && ball_y <= screen_height) { ballLeftOrRightHit(); } // 上碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y <= ball_r + brick_height) //、、、、、、、、、、、、、、、25号修改 { ballUpOrDownHit(); } // 下碰墙 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && ball_y >= screen_height-ball_r) { ballUpOrDownHit(); if(score<=10){ score = 0; } else score-=10; } } //-------------------墙壁碰撞检测结束------------------------------------- //-----------------------------挡板碰撞检测开始--------------------------- public void hitBoardCheck() { // 下碰挡板正面 if (ball_x >= ball_r && ball_x <= screen_width - ball_r && //在屏幕内,起码条件 ball_x >= board_left && ball_x <= board_right && //在挡板X域上方 ball_y == board_alterable_top - ball_r &&//球面与挡板相切 ball_previous_y <= board_alterable_top - ball_r //确定球是从上方下落 ) { if(board_alterable_top==board_default_top-boardYadd ){//如果弹簧伸张,挡板位于上线 ballHitBoardlower();//作用:ball_y_move减小;挡板被打下;小球Y向运动反向 } else if(board_alterable_top==board_default_top){//如果弹簧压缩,挡板位于下线 boardHitBallHigher();//作用:ball_y_move增加;挡板弹上;小球Y向运动反向 } } //斜碰挡板右上角//经验证有效 else if(Math.pow(board_right-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>board_right && ball_y<board_alterable_top) { ballcornerHit(); } //斜碰挡板的左上角//经验证有效 else if(Math.pow(board_left-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x<board_left && ball_y<board_alterable_top) { ballcornerHit(); } } //-----------------------------挡板碰撞检测结束-------------------------- private void boardHitBallHigher() {//增强 ballUpOrDownHit();//小球Y方向反向,ball_y_speed变为正数 if(ball_y_speed == ballXorYadd ){ ball_y_speed += ballXorYadd;//离开挡板后小球Y方向速度增强 } if( boardYadd > ball_y_speed ){//在线程这一轮,小球上升多少,挡板就上升多少。 board_alterable_top = board_default_top - ball_y_speed; board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } //检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线” private void boardPositionCheck() {//还可直接利用球的位置刷新,board_top与球心相差ball_r if(board_alterable_top < board_default_top && board_alterable_top > board_default_top-boardYadd){ //挡板随球上升 if(board_alterable_top - ball_y_speed >= board_default_top-boardYadd){ board_alterable_top -= ball_y_speed;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } else{ board_alterable_top = board_default_top-boardYadd;//挡板上层面 board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面 } } } private void ballHitBoardlower() {//减弱 board_alterable_top=board_default_top; board_alterable_bottom=board_default_top+board_thickness;//挡板被打退 ballUpOrDownHit();//小球Y方向反向 if(ball_y_speed==2*ballXorYadd){ ball_y_speed -= ballXorYadd;//小球Y方向速度减弱 } } //砖块碰撞检测开始----------------------------------------------------------- public void hitBrickCheck() { for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); //朝下碰砖的top面 AAAAAAAAAAAAAAAAAAAAA if(ball_x >= brick_left && ball_x <= brick_right && ball_y >= brick_top-ball_r && ball_y < brick_top) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝下正碰2砖中间,i砖右上角检测 if(k!=5 && ball_x == brick_right)//如果不是第5列砖的右侧边 { //如果砖[i+1]存在 if(brick_exist[i+1]){ brick_exist[i+1] = false; score+=4; } } //朝下正碰2砖中间,i砖左上角检测 else if(k!=1 && ball_x == brick_left)//如果不是第1列砖的左侧边 { //如果砖[i-1]存在 if(brick_exist[i-1]){ brick_exist[i-1] = false; score+=4; } } } //朝上碰砖的bottom面 BBBBBBBBBBBBBBBBB else if(ball_x >= brick_left && ball_x <= brick_right && ball_y > brick_bottom&& ball_y <= brick_bottom + ball_r ) { ballUpOrDownHit(); brick_exist[i] = false; score+=4; //朝上正碰2块砖中间--i砖的右下角检测 if(k!=5 && ball_x == brick_right) //如果不是第5列砖的右侧边 { if(brick_exist[i+1]){//如果砖[i+1]存在 brick_exist[i+1] = false; score+=4; } } //朝上正碰2块砖中间--i砖的左下角检测 else if(k!=1 && ball_x == brick_left) //如果不是第1列砖的左侧边 { if(brick_exist[i-1]){//如果砖[i-1]存在 brick_exist[i-1] = false; score+=4; } } } //朝右碰砖的left面CCCCCCCCCCCCCCCCC else if(ball_x >= brick_left -ball_r&&ball_x < brick_left&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝右正碰2块砖中间,左下角检测 if(j!=5 && ball_y == brick_bottom)//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝右正碰2块砖中间,左上角检测 else if(j!=1 && ball_y == brick_top)//如果不是第1行砖的上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } //朝左碰砖的right面DDDDDDDDDDDDDDDDDD else if(ball_x >= brick_right && ball_x <= brick_right+ball_r&& ball_y >= brick_top && ball_y <= brick_bottom) { ballLeftOrRightHit(); brick_exist[i] = false; score+=4; //朝左正碰2块砖中间,右下角检测 if(j!=5 && ball_y == brick_bottom )//如果不是第5行砖的下侧边 { if(brick_exist[i+5]){//如果砖[i+5]存在 brick_exist[i+5] = false; score+=4; } } //朝左正碰2块砖中间,右上角检测 else if(j!=1 && ball_y == brick_top )//如果不是第1行砖上侧边 { if(brick_exist[i-5]){//如果砖[i-5]存在 brick_exist[i-5] = false; score+=4; } } } ///////////////////////////////////////// //斜碰i砖的左下角EEEEEEEEEEEEEEEEEEEEEEEE else if(( i-1<0||(i-1>=0&&!brick_exist[i-1]) ) && (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_left-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_r && ball_x<brick_left && ball_y>brick_bottom && ball_y<brick_bottom+ball_r ) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右下角FFFFFFFFFFFFFFFFFFFFFFFFFF else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right&&ball_x<brick_right+ball_r&& ball_y>brick_bottom&&ball_y<brick_bottom+ball_r ) { Log.v("----------", "right bottom hit"+i+":"+brick_exist[i]); ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的右上角GGGGGGGGGGGGGGGGGGGGGGGG else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&& (i-5<0||(i-5>0&&!brick_exist[i-5]) )&& Math.pow(brick_right-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_right && ball_x<brick_right+ball_r&& ball_y>brick_top-ball_r && ball_y<brick_top) { ballcornerHit(); brick_exist[i] = false; score+=4; } //斜碰i砖的左上角HHHHHHHHHHHHHHHHHHHHHHHHHH else if((i-1<0||(i-1>=0&&!brick_exist[i-1])) && (i-5<0||(i-5>=0&&!brick_exist[i-5])) && Math.pow(brick_left-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&& ball_x>brick_left-ball_x && ball_x<brick_left && ball_y>brick_top-ball_r && ball_y<brick_top ) { ballcornerHit(); brick_exist[i] = false; score+=4; } }//end if }//end for }//end hitBrickCheck() //砖块碰撞检测结束----------------------------------------------------------- //------------------------------------------ public boolean gameOver(){ int count = 0; for(boolean s:brick_exist){ if(!s){ count++; } } if(count == 25){ return true; }else{ return false; } } //------------------------------------------ //---------------绘图方法开始------------------------ public void Draw() { // 锁定画布,得到canvas Canvas canvas = mSurfaceHolder.lockCanvas(); if (mSurfaceHolder == null || canvas == null) { return; } // 绘图 Paint mPaint = new Paint(); // 设置取消锯齿效果 mPaint.setAntiAlias(true); mPaint.setColor(Color.BLACK); // 绘制矩形--背景 canvas.drawRect(0, 0, screen_width, brick_height-2, mPaint); mPaint.setColor(Color.GREEN); // 绘制矩形--背景 canvas.drawRect(0, brick_height-2, screen_width, screen_height, mPaint); mPaint.setColor(Color.RED);//设置字体颜色 mPaint.setTextSize(brick_height-7);//设置字体大小 canvas.drawText("得分:"+score, 0, brick_height-7, mPaint); // 绘制顶层挡板````````````````````````````````````````````````````````25号修改 mPaint.setColor(Color.BLACK);//设置颜色 mPaint.setStrokeWidth(4);//设置粗细 canvas.drawLine(0, brick_height-2, screen_width, brick_height-2, mPaint); for (int i = 0; i <25 ; i++) { if (brick_exist[i]) { k = i % 5+1;// 1,2,3,4,5循环 j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5 brick_left = brick_width*(k-1); brick_right = brick_width*k; brick_top = brick_height*j; brick_bottom = brick_height*(j+1); mPaint.setStyle(Paint.Style.FILL);// 设置实心画笔 mPaint.setColor(Color.YELLOW); canvas.drawRect(brick_left+1, brick_top+1, brick_right-1,brick_bottom-1, mPaint); mPaint.setStyle(Paint.Style.STROKE);// 设置空心画笔 mPaint.setStrokeWidth(2);//设置粗细 mPaint.setColor(Color.BLUE); canvas.drawRect(brick_left+3, brick_top+3, brick_right-3,brick_bottom-3, mPaint); } } // 设置实心画笔 mPaint.setStyle(Paint.Style.FILL); { mPaint.setShader(mRadialGradient); canvas.drawCircle(ball_x, ball_y, ball_r, mPaint); } Paint mPaint2 = new Paint(); // 设置取消锯齿效果 mPaint2.setAntiAlias(true); // 设置实心画笔 mPaint2.setStyle(Paint.Style.FILL); { mPaint2.setColor(Color.BLACK); /* 绘制矩形挡板 */ canvas.drawRect(board_left, board_alterable_top, board_right, board_alterable_bottom,mPaint2); float board2_bottom = screen_height - 2*ball2_r;//164 float board2_top = board2_bottom - 4;//160 //线段端点坐标数组 float x0 = board_left+(board_right-board_left)/4; float y0 = board_alterable_bottom;//440或者444 float springAdd = (board2_top- board_alterable_bottom)/8;//即2.5或者2:弹簧小线段的y向长度 float springWidth = 5.0f;//弹簧小线段的x向长度 float x1 = x0 - springWidth; float y1 = y0 + springAdd; float x2 = x0 + springWidth; float y2 = y0 + 3*springAdd; float x3 = x1 ; float y3 = y0 + 5*springAdd; float x4 = x2; float y4 = y0 + 7*springAdd; float x5 = x0 ; float y5 = board2_top;//即460 float between_spring = (board_right-board_left)/2; float pts[] = { x0,y0,x1,y1, x1,y1,x2,y2, x2,y2,x3,y3, x3,y3,x4,y4, x4,y4,x5,y5}; float pts2[] = {x0+between_spring,y0, x1+between_spring,y1, x1+between_spring,y1, x2+between_spring,y2, x2+between_spring,y2, x3+between_spring,y3, x3+between_spring,y3, x4+between_spring,y4, x4+between_spring,y4, x5+between_spring,y5}; mPaint2.setStrokeWidth(2);//设置弹簧粗细 //绘制2个弹簧 canvas.drawLines(pts, mPaint2); canvas.drawLines(pts2, mPaint2); //绘制下层挡板 canvas.drawRect(board_left, board2_top, board_right, board2_bottom,mPaint2); mPaint2.setColor(Color.BLACK);// // 绘制最下面的两个“轮子”(圆心x,圆心y,半径r,p) canvas.drawCircle(board_left+ball2_r, screen_height-ball2_r, ball2_r, mPaint2);//圆 canvas.drawCircle(board_right-ball2_r, screen_height-ball2_r, ball2_r, mPaint2); mPaint2.setColor(Color.WHITE);// canvas.drawPoint(board_left+ball2_r, screen_height-ball2_r, mPaint2);//绘制左轮轮心 canvas.drawPoint(board_right-ball2_r, screen_height-ball2_r, mPaint2);//绘制右轮轮心 }//实心画笔mPaint2结束 if(gameOver()){ // mbLoop = false;//注释掉的话GAME OVER后也可重启 ball_isRun = false; mPaint2.setColor(Color.BLACK);//设置字体颜色 mPaint2.setTextSize(40.0f);//设置字体大小 canvas.drawText("GAME OVER", screen_width/32+40, screen_height/16*9-70, mPaint2); mPaint2.setTextSize(20.0f);//设置字体大小 canvas.drawText("Press \"MENU\" button to restart,", screen_width/32, //320/32=10 screen_height/16*9, //480/16=30,30*9=270 mPaint2); canvas.drawText("Press \"BACK\" button to exit.", screen_width/32, //320/32=10 screen_height/16*10, //480/16=30,30*10=300 mPaint2); } // 绘制后解锁,绘制后必须解锁才能显示 mSurfaceHolder.unlockCanvasAndPost(canvas); }// end of Draw() //---------------------绘图方法结束----------------------------------------------- // 在surface的大小发生改变时激发 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } // 在surface创建时激发 public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start();// 开启绘图线程 } // 在surface销毁时激发 public void surfaceDestroyed(SurfaceHolder holder) { // 停止循环 mbLoop = false; } } package sen.tan; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class dazhuankuaiActivity extends Activity { BallView myview; public static int screenWidth ; public static int screenHeight; private TextView textview; Button kaishi,jieshu,guanyu; ImageView image; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String string = "欢迎进入疯狂打砖块游戏"; image = (ImageView) this.findViewById(R.id.image); image.setImageResource(R.drawable.img1); textview =(TextView) this.findViewById(R.id.textview); textview.setTextSize(20); textview.setTextColor(Color.GREEN); textview.setBackgroundColor(Color.RED); textview.setText(string); kaishi=(Button)findViewById(R.id.button1); guanyu=(Button)findViewById(R.id.button2); jieshu=(Button)findViewById(R.id.button3); kaishi.setWidth(250); guanyu.setWidth(200); jieshu.setWidth(150); kaishi.setTextSize(35); guanyu.setTextSize(29); jieshu.setTextSize(24); jieshu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { dazhuankuaiActivity.this.finish(); } }); guanyu.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ textview.setText("本游戏由邵明森和谭明星制作,此游戏控制左右按键,来回移动挡板,不让小球掉落,当小球把上方砖块全部打完时,游戏胜利"); } }); kaishi.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ public void onCreate(Bundle savedInstanceState)这里显示的错误 显示的是上面两个括号标示不对。 { super.onCreate(savedInstanceState); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; setTitle("宽"+screenWidth+" 高"+screenHeight); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); myview = new BallView(this); setContentView(myview); } } public boolean onTouchEvent(MotionEvent event) { return true; } public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_MENU: myview.resetGame(); break; case KeyEvent.KEYCODE_DPAD_CENTER: myview.ball_isRun = !myview.ball_isRun; break; case KeyEvent.KEYCODE_DPAD_LEFT: if(myview.ball_isRun){ if(myview.board_left<=myview.board_x_move) { myview.board_left=0; myview.board_right=myview.board_length; }else{ myview.board_left-=myview.board_x_move; myview.board_right-=myview.board_x_move;} } break; case KeyEvent.KEYCODE_DPAD_RIGHT: if(myview.ball_isRun){ if(screenWidth-myview.board_right<=myview.board_x_move ) { myview.board_left=screenWidth-myview.board_length; myview.board_right=screenWidth; }else{ myview.board_left+=myview.board_x_move; myview.board_right+=myview.board_x_move; } } break; case KeyEvent.KEYCODE_DPAD_UP: if(myview.ball_isRun){ if( myview.board_alterable_top==myview.board_default_top) { myview.board_alterable_top-=myview.boardYadd; myview.board_alterable_bottom-=myview.boardYadd; } } break; case KeyEvent.KEYCODE_DPAD_DOWN: if(myview.ball_isRun){ if( myview.board_alterable_top==myview.board_default_top-myview.boardYadd ) { myview.board_alterable_top=myview.board_default_top; myview.board_alterable_bottom=myview.board_alterable_top+myview.board_thickness; } } break; case KeyEvent.KEYCODE_BACK: this.finish(); break; } return false; } public boolean onKeyUp(int keyCode, KeyEvent event) { return true; } public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) { return true; } } ); } }
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
【JSON解析】浅谈JSONObject的使用
简介 在程序开发过程中,在参数传递,函数返回值等方面,越来越多的使用JSON。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,同时也易于机器解析和生成、易于理解、阅读和撰写,而且Json采用完全独立于语言的文本格式,这使得Json成为理想的数据交换语言。 JSON建构于两种结构: “名称/值”对的集合(A Collection of name/va...
程序员请照顾好自己,周末病魔差点一套带走我。
程序员在一个周末的时间,得了重病,差点当场去世,还好及时挽救回来了。
卸载 x 雷某度!GitHub 标星 1.5w+,从此我只用这款全能高速下载工具!
作者 | Rocky0429 来源 | Python空间 大家好,我是 Rocky0429,一个喜欢在网上收集各种资源的蒟蒻… 网上资源眼花缭乱,下载的方式也同样千奇百怪,比如 BT 下载,磁力链接,网盘资源等等等等,下个资源可真不容易,不一样的方式要用不同的下载软件,因此某比较有名的 x 雷和某度网盘成了我经常使用的工具。 作为一个没有钱的穷鬼,某度网盘几十 kb 的下载速度让我...
只因接了一个电话,程序员被骗 30 万!
今天想给大家说一个刚刚发生在我身边的一起真实的诈骗经历,我的朋友因此被骗走30万。注:为了保护当事人隐私,部分情节进行了修改。1平安夜突来的电话开始以为就像普通的诈骗一样,想办法让你把钱...
我一个37岁的程序员朋友
周末了,人一旦没有点事情干,心里就瞎想,而且跟几个老男人坐在一起,更容易瞎想,我自己现在也是 30 岁了,也是无时无刻在担心自己的职业生涯,担心丢掉工作没有收入,担心身体机能下降,担心突...
python自动下载图片
近日闲来无事,总有一种无形的力量萦绕在朕身边,让朕精神涣散,昏昏欲睡。 可是,像朕这么有职业操守的社畜怎么能在上班期间睡瞌睡呢,我不禁陷入了沉思。。。。 突然旁边的IOS同事问:‘嘿,兄弟,我发现一个网站的图片很有意思啊,能不能帮我保存下来提升我的开发灵感?’ 作为一个坚强的社畜怎么能说自己不行呢,当时朕就不假思索的答应:‘oh, It’s simple. Wait for me for a ...
一名大专同学的四个问题
【前言】   收到一封来信,赶上各种事情拖了几日,利用今天要放下工作的时机,做个回复。   2020年到了,就以这一封信,作为开年标志吧。 【正文】   您好,我是一名现在有很多困惑的大二学生。有一些问题想要向您请教。   先说一下我的基本情况,高考失利,不想复读,来到广州一所大专读计算机应用技术专业。学校是偏艺术类的,计算机专业没有实验室更不用说工作室了。而且学校的学风也不好。但我很想在计算机领...
复习一周,京东+百度一面,不小心都拿了Offer
京东和百度一面都问了啥,面试官百般刁难,可惜我全会。
Java 14 都快来了,为什么还有这么多人固守Java 8?
从Java 9开始,Java版本的发布就让人眼花缭乱了。每隔6个月,都会冒出一个新版本出来,Java 10 , Java 11, Java 12, Java 13, 到2020年3月份,...
达摩院十大科技趋势发布:2020 非同小可!
【CSDN编者按】1月2日,阿里巴巴发布《达摩院2020十大科技趋势》,十大科技趋势分别是:人工智能从感知智能向认知智能演进;计算存储一体化突破AI算力瓶颈;工业互联网的超融合;机器间大规模协作成为可能;模块化降低芯片设计门槛;规模化生产级区块链应用将走入大众;量子计算进入攻坚期;新材料推动半导体器件革新;保护数据隐私的AI技术将加速落地;云成为IT技术创新的中心 。 新的画卷,正在徐徐展开。...
轻松搭建基于 SpringBoot + Vue 的 Web 商城应用
首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API ...
讲真,这两个IDE插件,可以让你写出质量杠杠的代码
周末躺在床上看《拯救大兵瑞恩》 周末在闲逛的时候,发现了两个优秀的 IDE 插件,据说可以提高代码的质量,我就安装了一下,试了试以后发现,确实很不错,就推荐给大家。 01、Alibaba Java 代码规范插件 《阿里巴巴 Java 开发手册》,相信大家都不会感到陌生,其 IDEA 插件的下载次数据说达到了 80 万次,我今天又贡献了一次。嘿嘿。 该项目的插件地址: https://github....
Python+OpenCV实时图像处理
目录 1、导入库文件 2、设计GUI 3、调用摄像头 4、实时图像处理 4.1、阈值二值化 4.2、边缘检测 4.3、轮廓检测 4.4、高斯滤波 4.5、色彩转换 4.6、调节对比度 5、退出系统 初学OpenCV图像处理的小伙伴肯定对什么高斯函数、滤波处理、阈值二值化等特性非常头疼,这里给各位分享一个小项目,可通过摄像头实时动态查看各类图像处理的特点,也可对各位调参、测试...
2020年一线城市程序员工资大调查
人才需求 一线城市共发布岗位38115个,招聘120827人。 其中 beijing 22805 guangzhou 25081 shanghai 39614 shenzhen 33327 工资分布 2020年中国一线城市程序员的平均工资为16285元,工资中位数为14583元,其中95%的人的工资位于5000到20000元之间。 和往年数据比较: yea...
为什么猝死的都是程序员,基本上不见产品经理猝死呢?
相信大家时不时听到程序员猝死的消息,但是基本上听不到产品经理猝死的消息,这是为什么呢? 我们先百度搜一下:程序员猝死,出现将近700多万条搜索结果: 搜索一下:产品经理猝死,只有400万条的搜索结果,从搜索结果数量上来看,程序员猝死的搜索结果就比产品经理猝死的搜索结果高了一倍,而且从下图可以看到,首页里面的五条搜索结果,其实只有两条才是符合条件。 所以程序员猝死的概率真的比产品经理大,并不是错...
害怕面试被问HashMap?这一篇就搞定了!
声明:本文以jdk1.8为主! 搞定HashMap 作为一个Java从业者,面试的时候肯定会被问到过HashMap,因为对于HashMap来说,可以说是Java集合中的精髓了,如果你觉得自己对它掌握的还不够好,我想今天这篇文章会非常适合你,至少,看了今天这篇文章,以后不怕面试被问HashMap了 其实在我学习HashMap的过程中,我个人觉得HashMap还是挺复杂的,如果真的想把它搞得明明白...
毕业5年,我问遍了身边的大佬,总结了他们的学习方法
我问了身边10个大佬,总结了他们的学习方法,原来成功都是有迹可循的。
python爬取百部电影数据,我分析出了一个残酷的真相
2019年就这么匆匆过去了,就在前几天国家电影局发布了2019年中国电影市场数据,数据显示去年总票房为642.66亿元,同比增长5.4%;国产电影总票房411.75亿元,同比增长8.65%,市场占比 64.07%;城市院线观影人次17.27亿,同比增长0.64%。 看上去似乎是一片大好对不对?不过作为一名严谨求实的数据分析师,我从官方数据中看出了一点端倪:国产票房增幅都已经高达8.65%了,为什...
推荐10个堪称神器的学习网站
每天都会收到很多读者的私信,问我:“二哥,有什么推荐的学习网站吗?最近很浮躁,手头的一些网站都看烦了,想看看二哥这里有什么新鲜货。” 今天一早做了个恶梦,梦到被老板辞退了。虽然说在我们公司,只有我辞退老板的份,没有老板辞退我这一说,但是还是被吓得 4 点多都起来了。(主要是因为我掌握着公司所有的核心源码,哈哈哈) 既然 4 点多起来,就得好好利用起来。于是我就挑选了 10 个堪称神器的学习网站,推...
这些软件太强了,Windows必装!尤其程序员!
Windows可谓是大多数人的生产力工具,集娱乐办公于一体,虽然在程序员这个群体中都说苹果是信仰,但是大部分不都是从Windows过来的,而且现在依然有很多的程序员用Windows。 所以,今天我就把我私藏的Windows必装的软件分享给大家,如果有一个你没有用过甚至没有听过,那你就赚了????,这可都是提升你幸福感的高效率生产力工具哦! 走起!???? NO、1 ScreenToGif 屏幕,摄像头和白板...
阿里面试,面试官没想到一个ArrayList,我都能跟他扯半小时
我是真的没想到,面试官会这样问我ArrayList。
曾经优秀的人,怎么就突然不优秀了。
职场上有很多辛酸事,很多合伙人出局的故事,很多技术骨干被裁员的故事。说来模板都类似,曾经是名校毕业,曾经是优秀员工,曾经被领导表扬,曾经业绩突出,然而突然有一天,因为种种原因,被裁员了,...
大学四年因为知道了这32个网站,我成了别人眼中的大神!
依稀记得,毕业那天,我们导员发给我毕业证的时候对我说“你可是咱们系的风云人物啊”,哎呀,别提当时多开心啦????,嗯,我们导员是所有导员中最帅的一个,真的???? 不过,导员说的是实话,很多人都叫我大神的,为啥,因为我知道这32个网站啊,你说强不强????,这次是绝对的干货,看好啦,走起来! PS:每个网站都是学计算机混互联网必须知道的,真的牛杯,我就不过多介绍了,大家自行探索,觉得没用的,尽管留言吐槽吧???? 社...
良心推荐,我珍藏的一些Chrome插件
上次搬家的时候,发了一个朋友圈,附带的照片中不小心暴露了自己的 Chrome 浏览器插件之多,于是就有小伙伴评论说分享一下我觉得还不错的浏览器插件。 我下面就把我日常工作和学习中经常用到的一些 Chrome 浏览器插件分享给大家,随便一个都能提高你的“生活品质”和工作效率。 Markdown Here Markdown Here 可以让你更愉快的写邮件,由于支持 Markdown 直接转电子邮...
【程序人生】程序员接私活常用平台汇总
00. 目录 文章目录00. 目录01. 前言02. 程序员客栈03. 码市04. 猪八戒网05. 开源众包06. 智城外包网07. 实现网08. 猿急送09. 人人开发10. 开发邦11. 电鸭社区12. 快码13. 英选14. Upwork15. Freelancer16. Dribbble17. Remoteok18. Toptal19. AngelList20. Topcoder21. ...
看完这篇HTTP,跟面试官扯皮就没问题了
我是一名程序员,我的主要编程语言是 Java,我更是一名 Web 开发人员,所以我必须要了解 HTTP,所以本篇文章就来带你从 HTTP 入门到进阶,看完让你有一种恍然大悟、醍醐灌顶的感觉。 最初在有网络之前,我们的电脑都是单机的,单机系统是孤立的,我还记得 05 年前那会儿家里有个电脑,想打电脑游戏还得两个人在一个电脑上玩儿,及其不方便。我就想为什么家里人不让上网,我的同学 xxx 家里有网,每...
史上最全的IDEA快捷键总结
现在Idea成了主流开发工具,这篇博客对其使用的快捷键做了总结,希望对大家的开发工作有所帮助。
阿里程序员写了一个新手都写不出的低级bug,被骂惨了。
这种新手都不会范的错,居然被一个工作好几年的小伙子写出来,差点被当场开除了。
谁是华为扫地僧?
是的,华为也有扫地僧!2020年2月11-12日,“养在深闺人不知”的华为2012实验室扫地僧们,将在华为开发者大会2020(Cloud)上,和大家见面。到时,你可以和扫地僧们,吃一个洋...
Idea 中最常用的10款插件(提高开发效率),一定要学会使用!
学习使用一些插件,可以提高开发效率。对于我们开发人员很有帮助。这篇博客介绍了开发中使用的插件。
AI 没让人类失业,搞 AI 的人先失业了
最近和几个 AI 领域的大佬闲聊 根据他们讲的消息和段子 改编出下面这个故事 如有雷同 都是巧合 1. 老王创业失败,被限制高消费 “这里写我跑路的消息实在太夸张了。” 王葱葱哼笑一下,把消息分享给群里。 阿杰也看了消息,笑了笑。在座几位也都笑了。 王葱葱是个有名的人物,21岁那年以全额奖学金进入 KMU 攻读人工智能博士,累计发表论文 40 余篇,个人技术博客更是成为深度学习领域内风向标。 ...
2020年,冯唐49岁:我给20、30岁IT职场年轻人的建议
点击“技术领导力”关注∆每天早上8:30推送 作者|Mr.K 编辑| Emma 来源|技术领导力(ID:jishulingdaoli) 前天的推文《冯唐:职场人35岁以后,方法论比经验重要》,收到了不少读者的反馈,觉得挺受启发。其实,冯唐写了不少关于职场方面的文章,都挺不错的。可惜大家只记住了“春风十里不如你”、“如何避免成为油腻腻的中年人”等不那么正经的文章。 本文整理了冯...
作为一名大学生,如何在B站上快乐的学习?
B站是个宝,谁用谁知道???? 作为一名大学生,你必须掌握的一项能力就是自学能力,很多看起来很牛X的人,你可以了解下,人家私底下一定是花大量的时间自学的,你可能会说,我也想学习啊,可是嘞,该学习啥嘞,不怕告诉你,互联网时代,最不缺的就是学习资源,最宝贵的是啥? 你可能会说是时间,不,不是时间,而是你的注意力,懂了吧! 那么,你说学习资源多,我咋不知道,那今天我就告诉你一个你必须知道的学习的地方,人称...
木兰编程语言,当事人最新回复来了
同行12年,不知Python是木兰,当事人回应来了
立即提问