Skip to content

Instantly share code, notes, and snippets.

@prespondek
Last active January 30, 2019 06:42
Show Gist options
  • Save prespondek/ca7c7cbead6531b7c953ac70808ef39d to your computer and use it in GitHub Desktop.
Save prespondek/ca7c7cbead6531b7c953ac70808ef39d to your computer and use it in GitHub Desktop.
Android development scratch pad
// Load image from assets folder
val tile = BitmapFactory.decodeStream(resources.assets.open("filename.png"), null, options)
// The amount canvas is scaling resource images (res/drawables)
var metrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(metrics)
val logicalDensity = metrics.density
// These options will scale bitmaps to fit your screen pixel density
var options = BitmapFactory.Options()
options.inDensity = DisplayMetrics.DENSITY_DEFAULT
options.inTargetDensity = DisplayMetrics.DENSITY_DEVICE_STABLE;
options.inScaled = true
// Multidirectional scrollview
class MDScrollView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : HorizontalScrollView(context, attrs, defStyleAttr)
{
var scrollView : ScrollView? = null
override fun onTouchEvent(event: MotionEvent?): Boolean {
super.onTouchEvent(event)
scrollView!!.onTouchEvent(event)
return true
}
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
super.onInterceptTouchEvent(ev)
scrollView!!.onInterceptTouchEvent(ev)
return true
}
}
// Activity
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val hv = MDScrollView(this)
val sv = ScrollView(this)
hv.scrollView = sv
addView(hv,ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
hv.addView(sv, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
}
//load json file as string
fun loadJSONAsset(file: String, context: Context): String? {
var json: String? = null
try {
val stream = context.getAssets().open(file)
val size = stream.available()
val buffer = ByteArray(size)
stream.read(buffer)
stream.close()
json = String(buffer, Charset.defaultCharset())
} catch (ex: IOException) {
ex.printStackTrace()
return null
}
return json
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment