Skip to content

Instantly share code, notes, and snippets.

@aliake9t9
Last active January 25, 2023 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliake9t9/98faf564066ebd247f6d6e7497571f8f to your computer and use it in GitHub Desktop.
Save aliake9t9/98faf564066ebd247f6d6e7497571f8f to your computer and use it in GitHub Desktop.
BtrArt1
package com.example.btrart1
import android.animation.TimeAnimator
import android.os.Bundle
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//タイトルバーを消す
supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
//
val myView=MyView(this)
setContentView(myView)
//アニメーションの設定
val anim =TimeAnimator()
val fpm=(1000/60).toLong()
anim.duration=fpm
anim.setTimeListener { _, _, _ ->
myView.invalidate()
myView.update()
}
anim.start()
}
}
package com.example.btrart1
import android.content.Context
import android.net.Uri
import android.media.MediaMetadataRetriever
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Matrix
import androidx.core.graphics.blue
import androidx.core.graphics.green
import androidx.core.graphics.red
//動画から画像を切り出す
class MovieToBmp {
var bmps= Array(4) { Array<Bitmap?>(360) { null } }
var initialized=false
//
fun set(context: Context){
val moviePath = Uri.parse("android.resource://"+context.packageName+"/"+R.raw.bocchi)
//val videoView=VideoView(context)
val ret=MediaMetadataRetriever()
//videoView.setVideoURI(moviePath)
ret.setDataSource(context,moviePath)
//var count = ret.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)!!.toInt()
val rate=0.4
//
for (i in 0 until 4) {
val j= arrayOf(2,4,5,7)[i]
var bmp=ret.getFrameAtTime(
(j * 1000 * 1000).toLong(),
MediaMetadataRetriever.OPTION_CLOSEST)
//リサイズ
bmp=Bitmap.createScaledBitmap(
//切り取り
Bitmap.createBitmap(
bmp!!,
bmp.width-bmp.height,
0,
bmp.height,
bmp.height),
(bmp.height*rate).toInt(),
(bmp.height*rate).toInt(),
true
).copy(Bitmap.Config.ARGB_8888,true)
//
bmp.setHasAlpha(true)
for(raw in 0 until bmp.width){
for(col in 0 until bmp.height){
val px=bmp.getPixel(raw,col)
if(px.green>256-96 && px.red<96 && px.blue <96){
bmp.setPixel(raw,col, Color.TRANSPARENT)
}
}
}
//
bmps[i][0]=bmp
}
//
ret.release()
}
fun createRotBmp(){
for(i in 0 until 4)
for(j in 0 until 360){
if (bmps[i][j] == null) {
val bmp=bmps[i][0]
val m=Matrix()
m.setRotate(j.toFloat())//Degree
bmps[i][j] = Bitmap.createBitmap(bmp!!,0,0, bmp.width, bmp.height,m,true)
}
}
//
}
//
fun get(kind:Int,deg:Int): Bitmap? {
return if (initialized) {
if (bmps[kind % 4][deg % 360] == null) {
val bmp=bmps[kind%4][0]
val m=Matrix()
m.setRotate(deg.toFloat())//Degree
bmps[kind % 4][deg % 360] = Bitmap.createBitmap(bmp!!,0,0, bmp.width, bmp.height,m,true)
}
bmps[kind % 4][deg % 360]
}
else {
null
}
}
}
package com.example.btrart1
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.core.graphics.createBitmap
import kotlin.math.*
import kotlin.random.Random
//描画本体
class MyView(context: Context): View(context){
//
private val paint: Paint = Paint()
//
private var m2b=MovieToBmp()
//
private val starsNum=40
private var stars= arrayListOf<Star>()
//
init {
//
Thread{
m2b.set(context)
m2b.createRotBmp()
m2b.initialized=true
}.start()
//
for(i in 0 until starsNum){
stars.add(Star(paint,m2b))
}
//
}
//描画関数
@RequiresApi(Build.VERSION_CODES.Q)
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas){
val bmp= createBitmap(width,height,Bitmap.Config.ARGB_8888)
val cbmp=Canvas()
//
cbmp.setBitmap(bmp)
cbmp.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR)
//
draw2(cbmp)
//背景塗りつぶし
canvas.drawColor(Color.WHITE)
canvas.drawBitmap(bmp,0F,0F,null)
//文字
//paint.color=Color.DKGRAY
//paint.style=Paint.Style.FILL_AND_STROKE
//paint.strokeWidth=5F
//paint.textSize=100F
//canvas.drawText("bocchi the rock", 0F, 100F,paint)
}
//描画する
@RequiresApi(Build.VERSION_CODES.Q)
private fun draw2(canvas: Canvas){
//
paint.style=Paint.Style.FILL_AND_STROKE
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.LIGHTEN)
for (i in 0 until starsNum)
for (j in i + 1 until starsNum)
for (k in j + 1 until starsNum) {
val s = stars[i]
val t = stars[j]
val u = stars[k]
if (s.kind == t.kind && t.kind == u.kind) {
if (s.distance(t) < 1000F && s.distance(u) < 1000F && t.distance(u) < 1000F) {
paint.color = s.col and 0x0FFFFFFF
val path = Path()
path.moveTo(s.x, s.y)
path.lineTo(t.x, t.y)
path.lineTo(u.x, u.y)
//path.close()//FILLのときは省略可
canvas.drawPath(path, paint)
}
}
}
//
paint.xfermode=null
//
//paint.style=Paint.Style.FILL_AND_STROKE
/*paint.color=0x8FFFFFFF.toInt()//Alphaは反映されるので注意
for(i in 0 until starsNum)
for(j in i+1 until starsNum){
val s=stars[i]
val t=stars[j]
if(s.kind==t.kind) continue
//
if(s.distance(t)<600F){
//グラデーション
val gradient=LinearGradient(s.x,s.y,t.x,t.y,
s.col,t.col,Shader.TileMode.CLAMP)
paint.shader = gradient
//
val w=1-cos(s.distance(t) / 600 * PI / 2)//0~2
paint.strokeWidth=(5.0+4*w*w).toFloat()
//canvas.drawLine(s.x,s.y,t.x,t.y,paint)
}
}
paint.shader = null
*/
//
for(s in stars){
s.drawCircle(canvas)
s.draw2(canvas)
}
}
//値更新
fun update(){
//
for (s in stars){
s.update(this)
}
}
//
class Star(val paint:Paint, val m2b:MovieToBmp){
//
var radius=Random.nextDouble(150.0,500.0)
var cx=Random.nextDouble(-900.0,900.0)
var cy=Random.nextDouble(-900.0,900.0)
var direction= if(Random.nextBoolean())1.0 else -1.0
var radian=Random.nextDouble(2*PI)
//
var cr=150F
var deg=Random.nextInt(360)
var degd=if(Random.nextBoolean())1 else -1
//
var x=0F
var y=0F
//
val kind= Random.nextInt(4)
val cols=arrayOf(Color.MAGENTA,Color.YELLOW,Color.CYAN,Color.RED)
var col=cols[kind]
//
var bmp:Bitmap?=null
//
fun draw2(canvas:Canvas){
if(bmp!=null){
canvas.drawBitmap(bmp!!, x - (bmp!!.width/2), y - (bmp!!.height/2), null)
}
}
@RequiresApi(Build.VERSION_CODES.Q)
fun drawCircle(canvas:Canvas){
//
paint.color=col and 0x4FFFFFFF
paint.style= Paint.Style.FILL
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.LIGHTEN)
//
canvas.drawCircle(x,y,cr,paint)
}
//
fun update(layout:View){
radian+=direction*2*PI*(1/radius)
deg=if(deg+degd>0)(deg+degd)%360 else 360+degd
//
x=(cos(radian)*radius+cx).toFloat()+layout.width/2
y= (sin(radian)*radius+cy).toFloat()+layout.height/2
//
if(m2b.initialized){
bmp=m2b.get(kind,deg)
}
}
//
fun distance(s:Star): Float {
val vx=x-s.x
val vy=y-s.y
return sqrt(vx*vx+vy*vy)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment