Android 自定义View之3D骰子旋转

你可以指定立方体中每一面骰子的点数,颜色和背景,同时也可以指定执行的动画时间和动画插值器

更多有趣的view

screenshot1.gif

screenshot2.gif

screenshot3.gif


使用

在根目录的build.gradle添加这一句代码:

allprojects {
    repositories {
        //...
        maven { url 'https://jitpack.io' }
    }
}

在app目录下的build.gradle添加依赖使用:

dependencies {
    implementation 'com.github.samlss:DiceLoadingView:1.0'
}


在开始介绍DiceLoadingView之前,先看一下骰子即DeiceView的组成

DiceView

在DiceLoadingView中应用的骰子view

以下为效果图:

screenshot4.png

使用

布局中:

<com.iigo.library.DiceView
            android:layout_marginTop="10dp"
            app:number="one"
            app:bgColor="@android:color/white"
            app:borderColor="@color/colorPrimary"
            app:pointColor="@color/colorPrimary"
            android:layout_width="50dp"
            android:layout_height="50dp" />

代码:

diceView.setNumber(2); //设置骰子点数,必须为1-6
diceView.setPointColor(Color.RED); //设置点的颜色
diceView.setBgColor(Color.RED); //设置背景颜色
diceView.setBorderColor(Color.RED); //设置边界颜色

属性说明:

开始描述效果图之前,先看一张说明图:

description2.png

attrdescription
number骰子点数 one(1), two(2), three(3), four(4), five(5), six(6)
pointColor点的颜色
bgColor背景颜色
borderColor边界颜色

DiceLoadingView

使用

布局中使用:

 <com.iigo.library.DiceLoadingView
                    android:id="@+id/dlv_loading1"
                    app:animDuration="2000"
                    app:animInterpolator="AccelerateDecelerateInterpolator"
                    app:firstSideDiceNumber="1"
                    app:firstSideDicePointColor="@color/colorPrimary"
                    app:firstSideDiceBgColor="@android:color/white"
                    app:firstSideDiceBorderColor="@color/colorPrimary"
                    app:secondSideDiceNumber="2"
                    app:secondSideDicePointColor="@android:color/holo_orange_dark"
                    app:secondSideDiceBgColor="@android:color/white"
                    app:secondSideDiceBorderColor="@android:color/holo_orange_dark"
                    app:thirdSideDiceNumber="3"
                    app:thirdSideDicePointColor="@android:color/holo_red_dark"
                    app:thirdSideDiceBgColor="@android:color/white"
                    app:thirdSideDiceBorderColor="@android:color/holo_red_dark"
                    app:fourthSideDiceNumber="4"
                    app:fourthSideDiceBgColor="@android:color/white"
                    app:fourthSideDiceBorderColor="@android:color/holo_green_dark"
                    app:fourthSideDicePointColor="@android:color/holo_green_dark"
                    android:layout_width="50dp"
                    android:layout_height="50dp" />


代码中使用:

diceLoadingView.start(); //开始动画
diceLoadingView.stop(); //停止动画
diceLoadingView.pause(); //暂停动画
diceLoadingView.resume(); //恢复动画
diceLoadingView.release(); //不需要使用该loading view的时候可手动释放,例如在activity的ondestroy()中

diceLoadingView.setDuration(3000); //设置动画时间
diceLoadingView.setInterpolator(new AnticipateOvershootInterpolator()); //设置动画插值器
diceLoadingView.setFirstSideDiceNumber(2); //设置第一面骰子点数(1-6)
diceLoadingView.setFirstSidePointColor(Color.parseColor("#FF7D81")); //设置第一面骰子点的颜色
diceLoadingView.setFirstSideDiceBgColor(Color.WHITE); //设置第一面骰子背景颜色
diceLoadingView.setFirstSideDiceBorderColor(Color.GRAY); //设置第一面骰子边界颜色

diceLoadingView.setSecondSideDiceNumber(3); //设置第二面骰子点数(1-6)
diceLoadingView.setSecondSidePointColor(Color.BLUE); //设置第二面骰子点的颜色
diceLoadingView.setSecondSideDiceBgColor(Color.WHITE); //设置第二面骰子背景颜色
diceLoadingView.setSecondSideDiceBorderColor(Color.BLUE); //设置第二面骰子边界颜色

diceLoadingView.setThirdSideDiceNumber(4); //设置第三面骰子点数(1-6)
diceLoadingView.setThirdSidePointColor(Color.GREEN); //设置第三面骰子点的颜色
diceLoadingView.setThirdSideDiceBgColor(Color.WHITE); //设置第三面骰子背景颜色
diceLoadingView.setThirdSideDiceBorderColor(Color.GREEN); //设置第三面骰子边界颜色

diceLoadingView.setFourthSideDiceNumber(5); //设置第四面骰子点数(1-6)
diceLoadingView.setFourthSidePointColor(Color.RED); //设置第四面骰子点的颜色
diceLoadingView.setFourthSideDiceBgColor(Color.WHITE); //设置第四面骰子背景颜色
diceLoadingView.setFourthSideDiceBorderColor(Color.RED); //设置第四面骰子边界颜色

属性说明:

开始描述效果图之前,先看两张张说明图:

针对立方体而言
description1.png

针对每一个骰子面而言:
description2.png

attrdescription
animDuration动画时间
animInterpolator动画加速器
firstSideDiceNumber第一面骰子点数(1-6)
firstSideDicePointColor第一面骰子点的颜色
firstSideDiceBgColor第一面骰子背景颜色
firstSideDiceBorderColor第一面骰子边界颜色
secondSideDiceNumber第二面骰子点数(1-6)
secondSideDicePointColor第二面骰子点的颜色
secondSideDiceBgColor第二面骰子背景颜色
secondSideDiceBorderColor第二面骰子边界颜色
thirdSideDiceNumber第三面骰子点数(1-6)
thirdSideDicePointColor第三面骰子点的颜色
thirdSideDiceBgColor第三面骰子背景颜色
thirdSideDiceBorderColor第三面骰子边界颜色
fourthSideDiceNumber第四面骰子点数(1-6)
fourthSideDicePointColor第四面骰子点的颜色
fourthSideDiceBgColor第四面骰子背景颜色
fourthSideDiceBorderColor第四面骰子边界颜色

Github

好的,下面是一个简单的Android OpenGL ES实现3D骰子的示例代码: 首先在build.gradle中添加OpenGL ES依赖: ```groovy implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.google.android.gms:play-services-gcm:16.0.0' implementation 'com.google.android.gms:play-services-ads:17.1.1' implementation 'com.google.android.gms:play-services-auth:16.0.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.squareup.okio:okio:1.14.0' implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:converter-gson:2.5.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'io.reactivex.rxjava2:rxjava:2.2.8' ``` 接着创建一个继承自GLSurfaceView自定义视图: ```java public class DiceView extends GLSurfaceView implements Renderer { private final static int DICE_NUM = 3; // 骰子数量 private final static float DICE_SIZE = 0.5f; // 骰子大小 private final static float DICE_SPEED = 0.1f; // 骰子旋转速度 private Dice[] dices = new Dice[DICE_NUM]; // 骰子数组 private float xAngle = 0; // x轴旋转角度 private float yAngle = 0; // y轴旋转角度 private float zAngle = 0; // z轴旋转角度 public DiceView(Context context) { super(context); setRenderer(this); setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { for (int i = 0; i < DICE_NUM; i++) { dices[i] = new Dice(getContext(), DICE_SIZE, DICE_SPEED); } gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glShadeModel(GL10.GL_SMOOTH); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); float ratio = (float) width / height; gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); } @Override public void onDrawFrame(GL10 gl) { gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0); gl.glRotatef(xAngle, 1, 0, 0); gl.glRotatef(yAngle, 0, 1, 0); gl.glRotatef(zAngle, 0, 0, 1); for (int i = 0; i < DICE_NUM; i++) { dices[i].draw(gl); } } public void setAngles(float xAngle, float yAngle, float zAngle) { this.xAngle = xAngle; this.yAngle = yAngle; this.zAngle = zAngle; } } ``` 然后创建一个Dice类来表示骰子: ```java public class Dice { private FloatBuffer vertexBuffer; private FloatBuffer textureBuffer; private ByteBuffer indexBuffer; private int[] textures = new int[1]; private int[] diceValues = {1, 2, 3, 4, 5, 6}; private float diceSize; private float diceSpeed; private float angleX = 0; private float angleY = 0; private float angleZ = 0; private float[] vertices = { -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f, -0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f }; private float[] textureCoords = { 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0 }; private byte[] indices = { 0, 1, 2, 0, 2, 3, 0, 3, 7, 0, 7, 4, 0, 4, 5, 0, 5, 1, 1, 5, 6, 1, 6, 2, 3, 2, 6, 3, 6, 7, 4, 7, 6, 4, 6, 5 }; public Dice(Context context, float diceSize, float diceSpeed) { this.diceSize = diceSize; this.diceSpeed = diceSpeed; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); vertexBuffer = vbb.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); ByteBuffer tbb = ByteBuffer.allocateDirect(textureCoords.length * 4); tbb.order(ByteOrder.nativeOrder()); textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textureCoords); textureBuffer.position(0); indexBuffer = ByteBuffer.allocateDirect(indices.length); indexBuffer.put(indices); indexBuffer.position(0); Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.dice); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); bitmap.recycle(); Random random = new Random(); setDiceValue(diceValues[random.nextInt(6)]); } public void draw(GL10 gl) { gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); gl.glPushMatrix(); gl.glRotatef(angleX, 1, 0, 0); gl.glRotatef(angleY, 0, 1, 0); gl.glRotatef(angleZ, 0, 0, 1); gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_BYTE, indexBuffer); gl.glPopMatrix(); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } public void setDiceValue(int value) { angleX = 0; angleY = 0; angleZ = 0; switch (value) { case 1: angleX = 90; break; case 2: angleX = -90; break; case 3: angleY = 90; break; case 4: angleY = -90; break; case 5: angleZ = 90; break; case 6: angleZ = -90; break; } } public void update() { angleX += diceSpeed; angleY += diceSpeed; angleZ += diceSpeed; } } ``` 最后在Activity中使用DiceView即可: ```java public class MainActivity extends AppCompatActivity { private DiceView diceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diceView = new DiceView(this); setContentView(diceView); } @Override protected void onResume() { super.onResume(); diceView.onResume(); } @Override protected void onPause() { super.onPause(); diceView.onPause(); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - lastX; float dy = y - lastY; diceView.setAngles(dy * 0.5f, dx * 0.5f, 0); diceView.requestRender(); break; } lastX = x; lastY = y; return true; } } ``` 这样就实现了一个简单的Android OpenGL ES实现3D骰子的程序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值