編輯:關於Android編程
效果圖:右邊的文字欄上下移動,沒有文字會自動停止移動。這和之前我寫的紋理移動不同,之前的是循環移動,這次是定位移動。
頂點著色器:
uniform mat4 uMVPMatrix; attribute vec3 aPosition; attribute vec2 aTexCoor; varying vec2 vTextureCoord; void main() { gl_Position=uMVPMatrix*vec4(aPosition,1); vTextureCoord=aTexCoor; }
precision mediump float; varying vec2 vTextureCoord; uniform sampler2D sTexture; uniform float uSpan; void main() { vec2 st_Result=vec2(0,0); st_Result.x=vTextureCoord.x; st_Result.y=vTextureCoord.y+uSpan; gl_FragColor=texture2D(sTexture,st_Result); }
package com.hl.paints; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import com.hl.utils.MatrixState; import android.opengl.GLES20; public class DrawRectMoveStop { int mProgram; int muMVPMatrixHandle; int maPositionHandle; int maTexCoorHandle; int muSpanHandle; FloatBuffer mVertexBuffer; FloatBuffer mTexCoorBuffer; int vCount=0; public DrawRectMoveStop(float width,float height,float s,float t,int mProgram) { // TODO Auto-generated constructor stub initVertex(width,height,s,t); initShader(mProgram); } private void initVertex(float width, float height,float s,float t) {// 紋理的傳入,目的是在最開始是不是將整個圖片放進矩形框中,而是一部分 // TODO Auto-generated method stub vCount = 6; float w = width / 2; float h = height / 2; float vertices[] = new float[] { -w, h, 0, -w, -h, 0, w, -h, 0, w, -h, 0, w, h, 0, -w, h, 0, }; ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); vbb.order(ByteOrder.nativeOrder()); mVertexBuffer = vbb.asFloatBuffer(); mVertexBuffer.put(vertices); mVertexBuffer.position(0); float texCoor[] = new float[] { 0, 0, 0, t, s, t, s, t, s, 0, 0, 0 }; ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4); cbb.order(ByteOrder.nativeOrder()); mTexCoorBuffer = cbb.asFloatBuffer(); mTexCoorBuffer.put(texCoor); mTexCoorBuffer.position(0); } private void initShader(int mProgram) { // TODO Auto-generated method stub this.mProgram = mProgram; muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor"); muSpanHandle=GLES20.glGetUniformLocation(mProgram, "uSpan"); } public void drawSelf(int texId,float currStart){ GLES20.glUseProgram(mProgram); GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0); GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3*4, mVertexBuffer); GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2*4, mTexCoorBuffer); GLES20.glEnableVertexAttribArray(maPositionHandle); GLES20.glEnableVertexAttribArray(maTexCoorHandle); GLES20.glUniform1f(muSpanHandle, currStart); GLES20.glActiveTexture(GLES20.GL_TEXTURE0); GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId); GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount); } }
private DrawRectMoveStop benRightText; BUTTON_BEN_RIGHT3_WIDTH = 2.0f * ratio * 0.23f; BUTTON_BEN_RIGHT3_HEIGHT = 1.4f; BUTTON_BEN_RIGHT3_XOFFSET = ratio - 2.0f * ratio * 0.23f / 2; BUTTON_BEN_RIGHT3_YOFFSET = 1.0f - 0.15f - 0.37f - 0.02f - 0.7f; benRightText = new DrawRectMoveStop(BUTTON_BEN_RIGHT3_WIDTH, BUTTON_BEN_RIGHT3_HEIGHT, 1.0f, 0.7f, ShaderManager.getMoveTextureShaderProgram());
//1.0f and 0.7f 是根據紋理圖片和寬度計算的。效果圖中的右邊文字部分,是圖片形式的。
MatrixState.pushMatrix(); MatrixState.translate(BUTTON_BEN_RIGHT3_XOFFSET, BUTTON_BEN_RIGHT3_YOFFSET, 0); benRightText.drawSelf(rText[condition], textYOffset); MatrixState.popMatrix();private float textYOffset = 0;
if (UtilConfigArea.isInArea(x, y, AREA_BEN_RIGHT3)) {//onTouchEvent ACTION_MOVE: textYOffset -= dy * TOUCH_SCALE_FACTOR * 0.002f; if (textYOffset > 0.3f) { textYOffset = 0.3f; } if (textYOffset < 0.0f) { textYOffset = 0.0f; } }
注:本文裡面用到一些方法,在我的其它博文中有提到,若用到,請查相關博文。
請查看下面這個視頻作為本教程最終的結果。從這裡開始,空白 Activity 裡面有一個 DrawerLayout 。 Activity 已經調整為材料設計風格的
華為榮耀於8月1號下午正式發布了6.6吋大屏手機華為榮耀NOTE8,對於買到新機的朋友是不是有這樣的疑問呢,華為榮耀note8怎麼裝sim卡?華為榮耀not
1、前言 等級信號狀態的View在現在的Android系統中非常的常見,比如手機右上角的電池狀態的圖標就非常的經典,有幾種狀態,到了快沒電的時候有些還會閃爍提示用戶充電;
概述2014年,Google攜Android5.X重裝歸來,全新的UI設計和更加優化的性能,令開發者眼前一亮安裝和配置Android5.0開發環境開發Android還得靠