Last active
January 30, 2019 06:42
-
-
Save prespondek/ca7c7cbead6531b7c953ac70808ef39d to your computer and use it in GitHub Desktop.
Android development scratch pad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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