AndroidStudio3.3版本的一个问题
2048游戏 :
gameview.java
public class GameView extends GridLayout {
public GameView(Context context) {
super(context);
initGameView();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
initGameView();
}
public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initGameView();
}
private void initGameView(){
setColumnCount(4);
setOnTouchListener(new OnTouchListener() {
private float downX,downY,offsetX,offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
downY=event.getY();
break;
case MotionEvent.ACTION_UP:
offsetX=event.getX()-downX;
offsetY=event.getY()-downY;
if(Math.abs(offsetX)>Math.abs(offsetY)){
if(offsetX<-5){
swipeLeft();
}else if (offsetX>5){
swipeRight();
}
}else{
if(offsetY<-5){
swipeUp();
}else if (offsetY>5){
swipeDown();
}
}
break;
default:
break;
}
return true;
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int cardWith=(Math.min(w,h)-10)/4;
addCards(cardWith,cardWith);
}
private void addCards(int cardWith,int cardHeight){
Card c;
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
c=new Card(getContext());
c.setNum(2);
addView(c,cardWith,cardHeight);
}
}
}
}
card.java
public class Card extends FrameLayout {
private int num=0;
private TextView lable;
public Card(@NonNull Context context) {
super(context);
lable = new TextView(getContext());
lable.setTextSize(30);
LayoutParams lp = new LayoutParams(-1, -1);
addView(lable, lp);
setNum(0);
}
public int getNum(){
return this.num;
}
public void setNum(int num){
this.num=num;
lable.setText(""+num);
}
public boolean equeals(Card o){
return getNum()==o.getNum();
}
}
在gameview.java中添加card 没有文本显示,只有gameview的框架
debug结果:
2019-03-15 09:28:07.223 1640-1651/? W/android.os.Debug: failed to get memory consumption info: -1
2019-03-15 09:28:07.482 1640-2100/? E/memtrack: Couldn't load memtrack module
gameview.java
public class GameView extends GridLayout {
public GameView(Context context) {
super(context);
initGameView();
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
initGameView();
}
public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initGameView();
}
private void initGameView(){
setColumnCount(4);
setOnTouchListener(new OnTouchListener() {
private float downX,downY,offsetX,offsetY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
downY=event.getY();
break;
case MotionEvent.ACTION_UP:
offsetX=event.getX()-downX;
offsetY=event.getY()-downY;
if(Math.abs(offsetX)>Math.abs(offsetY)){
if(offsetX<-5){
swipeLeft();
}else if (offsetX>5){
swipeRight();
}
}else{
if(offsetY<-5){
swipeUp();
}else if (offsetY>5){
swipeDown();
}
}
break;
default:
break;
}
return true;
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int cardWith=(Math.min(w,h)-10)/4;
addCards(cardWith,cardWith);
}
private void addCards(int cardWith,int cardHeight){
Card c;
for(int y=0;y<4;y++){
for(int x=0;x<4;x++){
c=new Card(getContext());
c.setNum(2);
addView(c,cardWith,cardHeight);
}
}
}
}
card.java
public class Card extends FrameLayout {
private int num=0;
private TextView lable;
public Card(@NonNull Context context) {
super(context);
lable = new TextView(getContext());
lable.setTextSize(30);
LayoutParams lp = new LayoutParams(-1, -1);
addView(lable, lp);
setNum(0);
}
public int getNum(){
return this.num;
}
public void setNum(int num){
this.num=num;
lable.setText(""+num);
}
public boolean equeals(Card o){
return getNum()==o.getNum();
}
}
在gameview.java中添加card 没有文本显示,只有gameview的框架
debug结果:
2019-03-15 09:28:07.223 1640-1651/? W/android.os.Debug: failed to get memory consumption info: -1
2019-03-15 09:28:07.482 1640-2100/? E/memtrack: Couldn't load memtrack module
...全文
相关推荐
3.6.3新版本AndroidStudio报Could not resolve all artifacts for configuration ‘:app:classpath’等系列问题解决方法
最近因为换了个台式电脑,重新下载了最新的android studio ,2020年5月6号的3.6.3版本, 结果编译工程和新建工程,都直接就报了以下错误: Could not resolve all dependencies for configuration ‘:classpath’. Could not resolve com.android.tools.build:gradle:2.1.2. ………. 等6个error 上网百度csdn,结果试下各种方法,依旧没用。 搞了一天,直接说结果,我这边时用户名时中文的原因。我解决的办法时重新安装了系统,用户名改成了英文。 以下我总结了: 我
AndroidStudio教程
AndroidStudio教程。
经典项目——AndroidStudio版本.zip
经典项目——AndroidStudio版本.zip经典项目——AndroidStudio版本.zip
androidstudio3.0-jre
androidstudio3.0 输入法问题解决,解压放到androidstudio安装目录替换掉即可!
androidstudio导入代码工具.zip
androidstudio导入源码的工具,内含readme说明,操作简单,实用


