这主要是分析各部分代码,推荐链接:https://blog.csdn.net/qq_43433255/article/details/87939060
现在这主要是,将全部代码推出来,当然,嫌弃麻烦的,也可以直接下载:https://download.csdn.net/download/qq_43433255/11206184
代码的主要构成:
文章目录
一、Java中代码构成
1.1、Mainactivity的代码
package com.example.wuluo.youxitwo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public MainActivity() {
mainActivity = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout) findViewById(R.id.container);
root.setBackgroundColor(0xfffaf8ef);
tvScore = (TextView) findViewById(R.id.tvScore);
tvBestScore = (TextView) findViewById(R.id.tvBestScore);
gameView = (Gameview) findViewById(R.id.gameView);
btnNewGame = (Button) findViewById(R.id.btnNewGame);
btnNewGame.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {
gameView.startGame();
}});
animLayer = (AnimLayer) findViewById(R.id.animLayer);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void clearScore(){
score = 0;
showScore();
}
public void showScore(){
tvScore.setText(score+"");
}
public void addScore(int s){
score+=s;
showScore();
int maxScore = Math.max(score, getBestScore());
saveBestScore(maxScore);
showBestScore(maxScore);
}
public void saveBestScore(int s){
Editor e = getPreferences(MODE_PRIVATE).edit();
e.putInt(SP_KEY_BEST_SCORE, s);
e.commit();
}
public int getBestScore(){
return getPreferences(MODE_PRIVATE).getInt(SP_KEY_BEST_SCORE, 0);
}
public void showBestScore(int s){
tvBestScore.setText(s+"");
}
public AnimLayer getAnimLayer() {
return animLayer;
}
private int score = 0;
private TextView tvScore,tvBestScore;
private LinearLayout root = null;
private Button btnNewGame;
private Gameview gameView;
private AnimLayer animLayer = null;
private static MainActivity mainActivity = null;
public static MainActivity getMainActivity() {
return mainActivity;
}
public static final String SP_KEY_BEST_SCORE = "bestScore";
}
1.2、AnimLayer的代码
package com.example.wuluo.youxitwo;
/**
* Created by wuluo on 2019/5/26
*/
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
public class AnimLayer extends FrameLayout {
public AnimLayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initLayer();
}
public AnimLayer(Context context, AttributeSet attrs) {
super(context, attrs);
initLayer();
}
public AnimLayer(Context context) {
super(context);
initLayer();
}
private void initLayer(){
}
public void createMoveAnim(final Card from,final Card to,int fromX,int toX,int fromY,int toY){
final Card c = getCard(from.getNum());
LayoutParams lp = new LayoutParams(Config.CARD_WIDTH, Config.CARD_WIDTH);
lp.leftMargin = fromX*Config.CARD_WIDTH;
lp.topMargin = fromY*Config.CARD_WIDTH;
c.setLayoutParams(lp);
if (to.getNum()<=0) {
to.getLabel().setVisibility(View.INVISIBLE);
}
TranslateAnimation ta = new TranslateAnimation(0, Config.CARD_WIDTH*(toX-fromX), 0, Config.CARD_WIDTH*(toY-fromY));
ta.setDuration(100);
ta.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {}
@Override
public void onAni